Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Find the top 5 maximum values in array of structures (for C programming)? I have

ID: 3815927 • Letter: F

Question

Find the top 5 maximum values in array of structures (for C programming)?

I have an array of structures as follows:

struct info {

char name[100];

int number;

}

struct info people[10]

In char name[100] are names of people (up to 10) and they have a corresponding value in int balance:

Jane 10

John 40

Harry 16

Eva -5

...

until it gets to 10 people.

How do I find and print the 5 people who have the highest numbers? i.e:

John 40

Harry 16

Jane 10

...

I have tried the following code:

int i,j, k=5, max, temp;

//move maximum 5 numbers to the front of the array

However, I get an error message on the second swap since its char type. How should I fix this or what else would work for this objective? Also, how would I write this as a function?

Explanation / Answer


You can not swap the character array using = operator.
You have to user string function: strcpy;

//This is Wrong
temp=people[i].name;
people[i].name=people[max].name;
people[max]=temp;


THis is the correct way:
char temp[100];
strcpy(temp, people[i].name);
strcpy(people[i].name, people[max].name);
strcpy(people[max].name, temp)

Add: #include <string.h> in top

Please let me know in case of any issue

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote