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.
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:
Then do the following:
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:
size
variable in 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.
345 Who developed the "avformat-59.dll" that comes with FFmpeg?
351 I have saved several codes written in python to a visual studio file.
361 Add dataframe values of the same structure without column names
355 Unity Virtual Stick Does Not Return to Center When You Release Your Finger
362 To Limit Column Values to Strings in a String List Using sqlalchemy
© 2023 OneMinuteCode. All rights reserved.