I want to write a function which helps the user to insert N number of people, with their names and age.
For example:
4
John Williams 37
Michael Douglas 65
Will Smith 51
Clark Kent 33
Then, I've to find the oldest one based on the age
and print the name and age:
Michael Douglas 65
EDIT:
I have a NEW code which is this one:
#include <stdio.h>
int main()
{
char peopleName[5][20],peopleAge[5];
int i;
int maxAge=0, maxName=-1;
for(i=0;i<5;i++)
{
printf("Name & Age %d :",i+1);
scanf("%s",&peopleName[i]);
scanf("%d",&peopleAge[i]);
if(peopleAge[i]>maxAge)
{
maxAge=peopleAge[i];
maxName=i;
}
}
printf("%s %d", peopleName[maxName],peopleAge[maxAge]);
}
My question is: How can I change from having 5 people to N number of people (what I mean is, How can I choose the number of people I can input)?
You need to ask the user for the number that they want to insert. Once given this you need to set the size of your arrays because you do not know their size until afterward
#include <stdio.h>
int main() {
int i;
int maxAge = 0, maxName = -1;
int numberOfPeople = 0;
scanf("%d", & numberOfPeople);
//now we have to set the arrays to the size that we are inserting
char peopleName[numberOfPeople][20], peopleAge[numberOfPeople];
for (i = 0; i < numberOfPeople; i++) {
printf("Name & Age %d :", i + 1);
scanf("%s", & peopleName[i]);
scanf("%d", & peopleAge[i]);
if (peopleAge[i] > maxAge) {
maxAge = i; //you had the actual age here,
//but you need the index of the highest age instead
maxName = i;
}
}
printf("%s %d", peopleName[maxName], peopleAge[maxAge]);
}
This should be what you want:
#include <stdio.h>
#define MAX_PEOPLE 100
#define MAX_NAME 100
int main(void)
{
char peopleName[MAX_PEOPLE][MAX_NAME];
int peopleAge[MAX_PEOPLE]; // a person's age is an integer
int n, i;
int maxAge = 0, maxName = -1;
puts("How many people do you want to input?");
scanf("%d%*c", &n); // discarding '\n'
if(n > MAX_PEOPLE)
puts("Too many people!");
for(i = 0; i < n; i++)
{
printf("Name & Age %d :", i + 1);
scanf(" %s", peopleName[i]);
scanf(" %d", &peopleAge[i]); // discarding whitespace characters
if(peopleAge[i] > maxAge)
{
maxAge = peopleAge[i];
maxName = i;
}
}
printf("%s %d", peopleName[maxName], maxAge); // maxAge is a value, rather than an index
}
See my comments for illustration. In fact, there are some issues in your code, so I fixed them.
I changed a little bit, but I think it will help to see in a simpler way.
#include <stdio.h>
int main(void) {
int indexOfMaxAge = 0, numberOfPeople = 0;
scanf("%d", &numberOfPeople);
char peopleName[numberOfPeople][20], peopleAge[numberOfPeople];
for (int i = 0; i < numberOfPeople; i++) {
printf("Name & Age %d :", i + 1);
scanf("%s", &peopleName[i]);
scanf("%d", &peopleAge[i]);
if (peopleAge[i] > peopleAge[indexOfMaxAge]) indexOfMaxAge = i;
// only the index of the older person
// is being stored and used to compare
}
printf("%s %d\n", peopleName[indexOfMaxAge], peopleAge[indexOfMaxAge]);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.