I want to create a program that removes the maximum value from the array.

Asked 1 months ago, Updated 1 months ago, 1 views

I have a question for my school assignment.
I'd like to create a function program called deleteidx that erases the maximum value, but I don't know how to create it.
For the time being, here are the parts that I have programmed at this time.

#include<stdio.h>

void readIntArray(inta[], int size)
{

    inti;
    for(i=0;i<10;i=i+1){
        printf("Input A[%d]:", i);
        scanf("%d", & a[i]);
    }
}

void printIntarray(int data[], int size)
{
   inti;
   printf("{");
   for(i=0;i<size;i++){
      printf("%3d", data[i]);
      if(i+1!=size)printf(", ");
     }
    printf("}\n");
}

void getMaxIdx(int data[], int size, int*maxidx)
{
    inti, index;
    * maxidx = data[0];
    for(i=0;i<size;i++){
        if(data[i]>*maxidx){
            *maxidx=data[i];
            index=i;
        }
    }
}

// I think I should insert deleteidx here

int main (void)
{
    int size,d[100];
    inti,max;

    readIntArray(d,size);
    printf("Before:");
    printIntarray(d,size);   

    printf("After:");

    return 0;
}

Expected Execution Results

Input A[0]:1
Input A[1]—2
Input A[2]—3
Input A[3]—4
Input A[4]—5
Input A[5]—6
Input A[6]—7
Input A [7]—8
Input A[8]—9
Input A [9]: 10
Before: {1,2,3,4,5,6,7,8,9,10}
After: {1,2,3,4,5,6,7,8,9}

"Explanation of ""deleteidx"" features and algorithms given by the teacher
" deleteidx description
Arguments: int data[], int size, int idx
Delete elements of idx subscript number for integer array data with number of elements * size
Change the size value after deleting the element.


1. An array for data of length 10 is inputted.
2. Display the array before deletion.
3. Call getMaxIdx to find the subscript number of the element with the maximum value
4. Call deleteidx to delete the element with the maximum value.
5.Display the array after deletion.

First of all, I got these tips and cleared algorithm 1. I don't know how to handle it after that.

How do I program it to achieve the desired execution results?
Thank you for your cooperation.

c

2022-09-30 11:04

1 Answers

The idea is, if you have data behind a given index number, you can move all of that data to the previous index and reduce the size by one.

If you don't have any data behind you, you just need to reduce the size by one.

add
Also, if @cubick allows multiple maximums to exist as you commented, the way of thinking will change, for example:
Define and initialize the following variables:

  • v:Working variable that saves the maximum value stored at the specified index (initial value: maximum value in array)
  • i: Index value variable for loop to check from beginning to end of array (initial value: 0)
  • n—Index value variable (initial value: 0)
  • indicating where to copy a number that is not the maximum value since the maximum value was present.

Then do the following:

  • Repeat the index value i for the loop from the 0th to the size-1st and compare the position value to the saved maximum value v
  • If the i-th value to be checked is not the maximum value, copy it to the location of the destination index n and add n to +1.
  • Make the value of n the value of the new size after checking it to the end

By the way, I am confused whether the size parameter is the value or the pointer to the size variable.
Please check which one it is and correct it.

Arguments: int data[], int size, int idx
Delete idx subscript number elements for integer array data with number of elements * size

Depending on this, the following actions will change:

Change the size value after deleting the element.

Also, the source at the time of the question has the following issues:

    The size variable in
  • main() is not initialized
  • main() has d array elements 100 instead of 10
  • void readIntArray(inta[], int size)'s int size parameter is empty and meaningless.
  • voidgetMaxIdx(int data[], int size, int*maxidx) after processing int*maxidx (max of main()) is not the index value of the element having the maximum value, but the maximum value itself.
    In other words,
    processing is not performed to find the subscript number of the element with the maximum value.


2022-09-30 11:04

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.