简体   繁体   中英

Find Biggest Number in C, by N number of inputs

So I have this code:

#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]);
}

This code finds from 5 people the oldest one. I want to change from 5 people to N number of people, whatever the number I input myself. (For example I put 7 , and I can insert seven names and ages and so on).

You can use malloc to allocate buffer dynamically.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    char (*peopleName)[20];
    int *peopleAge;
    int i;
    int maxAge=0, maxName=-1;
    int dataNum;
    printf("How many people? :");
    if(scanf("%d",&dataNum)!=1)return 1;
    peopleName=malloc(sizeof(char[20])*dataNum);
    peopleAge=malloc(sizeof(int)*dataNum);
    for(i=0;i<dataNum;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[maxName]);
    free(peopleName);
    free(peopleAge);
    return 0;
}

Also please note that:

  • You should pass char* , not char(*)[20] , for %s in scanf
  • peopleAge[maxAge] may be out of bounds. maxName (or other name but same role) should suit here.

You can do this -

int n;                                  //  number of people
scanf("%d",&n);                         // take input from user
char peopleName[n][20],peopleAge[n];    //  declare 2-d array 

for(i=0;i<n;i++)
{
  // your code
}

Also this statement -

scanf("%s",&peopleName[i]);    // pass char * as argument to %s

should be -

scanf("%19s",peopleName[i]);   // one space is left for null character

The question has two parts: How does the user specify how many persons are entered? And how do I store the data?

The second part is easy: No matter how many persons you are going to consider, if you just want to know who is the oldest, it is enough to keep the name and age of the currently oldest person. (Of course, if there is a tie and many people are, say, 80 years old, you just get to keep the first match.)

Not storing anything also simplifies the first question. You could ask the user to specify the number of persons beforehand and that's find if you have few people. If you have a list of many people, the user would have to count the by hand and then enter the count. Miscounting is very likely.

A better way is to indicate the end of input by another means, for example by a negative age or by two dashes as name. There is also the possibility that the input runs out, for example when redirecting input from a file or when pressing one of Ctrl-Z or Ctrl-D, depending on your platform, after the input.

The example below read the input line-wise and then scans that line. The loop while (1) is in theory infinite, in practice execution breaks out of the loop when the input runs out – fgets return NULL –, when a blank line is read or when the input isn't in the format single-word name and age.

#include <stdio.h>

int main(void)
{
    char oldest[80] = "no-one";
    int max_age = -1;
    int count = 0;

    puts("Enter name & age on each line, blank line to stop:");

    while (1) {
        char line[80];
        char name[80];
        int age;

        if (fgets(line, sizeof(line), stdin) == NULL) break;
        if (sscanf(line, "%s %d", name, &age) < 2) break;

        if (age > max_age) {
            strcpy(oldest, name);
            max_age = age;
        }

        count++;
    }

    printf("The oldest of these %d people is %s, aged %d.\n", 
        count, oldest, max_age);

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM