Home > Software design >  Problem with counting elements in the list of names
Problem with counting elements in the list of names

Time:11-22

I have made one program, where you enter a few characters (10 max). It makes you a list, count average length of surnames, tell about how much different names. But the problem is, when I enter the last number (10) - it sorts me it incorrectly (like 1, 10, 2, 3, 4, 5, 6, 7, 8, 9). Beneath I will present my code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct people {
        char num[2];
        char surname[20];
        char name[10];
    } peoples[10], c;
    
int main()
{
    int i, j, k = 0, l = 0, m = 0, n = 0;
    float  s = 0;
    char str[100];
    system("chcp 1251 > nul");
    
    for (i = 0, j = 0; i < 10; i  , j  )
    {
        printf("Enter number, surname, name %d of your human: ", i   1);
        gets(str);
        
        while (str[n] != '\n')
        {
            
            if (str[n] != ' ')
            {
                peoples[j].num[k] = str[n];
            }
            else
                break;
                
            n  ;
            k  ;
        }
        n  ;
        k = 0;
        while (str[n] != '\n')
        {
            
            if (str[n] != ' ')
            {
                peoples[j].surname[k] = str[n];
            }
            else
                break;
                
            n  ;
            k  ;
        }
        n  ;
        k = 0;
        while (str[n] != '\n')
        {
            
            if (str[n] != '\0')
            {
                peoples[j].name[k] = str[n];
            }
            else
                break;
                
            n  ;
            k  ;
        }
        n = 0;
        k = 0;
    }
    
    for (i = 0; i < 10; i  )
    {
        for (j = i   1; j < 10; j  )
        {
            if (!strcmp(peoples[i].name, peoples[j].name))
                m = 1;
        }
        if (m == 0)
            l  ;
        
        m = 0;
        
        s = s   strlen(peoples[i].surname);
    }
    
    for (i = 0; i < 9; i  )
    
        for (j = 0; j < 9; j  )
        
            if (strcmp(peoples[j].num, peoples[j 1].num) > 0)
            {
                c = peoples[j 1];
                peoples[j 1] = peoples[j];
                peoples[j] = c;
            }
        
    for (i = 0; i < 10; i  )
    {
        printf("%s ", peoples[i].num);
        printf("%s ", peoples[i].name);
        printf("%s ", peoples[i].surname);
        printf("\n");
    }
    
    printf("\nYou have %d different names\n", l);
    printf("Avarege lenght of surname is = %f\n", s / 10);
}

CodePudding user response:

If you want to give numeric input, then use actual numeric data.

Change the num field to become an int instead of a single-character string:

struct people {
    int num;
    char surname[20];
    char name[10];
};

Use fgets to read the line:

fgets(str, sizeof str, stdin);

[Error checking left as exercise for reader]

Then use e.g. sscanf for parse your string:

sscanf(str, "%d %s %s", &peoples[j].num, &peoples[j].name, &peoples[j].name);

[Error checking left as exercise for reader]

And finally, instead of doing your own sorting use the standard qsort function:

qsort(peoples, 10, sizeof(struct people), &compare_people_num);

With the comparison function being something like this:

int compare_people_num(const void *a, const void *b)
{
    const struct people *p1 = a;
    const struct people *p2 = b:

    return p1->num - p2->num;  // Change order to reverse sort
}

Using sscanf and qsort will make your code much simpler and easier to understand.

CodePudding user response:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
struct people {
        int num[10];
        char surname[20];
        char name[10];
    } peoples[10], c;

int compare_people_num(const void *a, const void *b);
    
int main()
{ 
    int i, j, k = 0, l = 0, m = 0, n = 0;
    float  s = 0;
    char str[100];
    system("chcp 1251 > nul");
    
    for (i = 0, j = 0; i < 10; i  , j  )
    {
        printf("Enter number, surname and name %d of the human: ", i   1);
        fgets(str, sizeof str, stdin);
        sscanf(str, "%d %s %s", &peoples[j].num, &peoples[j].name, &peoples[j].name);
        
        while (str[n] != '\n')
        {
            
            if (str[n] != ' ')
            {
                peoples[j].num[k] = str[n];
            }
            else
                break;
                
            n  ;
            k  ;
        }
        n  ;
        k = 0;
        while (str[n] != '\n')
        {
            
            if (str[n] != ' ')
            {
                peoples[j].surname[k] = str[n];
            }
            else
                break;
                
            n  ;
            k  ;
        }
        n  ;
        k = 0;
        while (str[n] != '\n')
        {
            
            if (str[n] != '\0')
            {
                peoples[j].name[k] = str[n];
            }
            else
                break;
                
            n  ;
            k  ;
        }
        n = 0;
        k = 0;
    }
    
    for (i = 0; i < 10; i  )
    {
        for (j = i   1; j < 10; j  )
        {
            if (!strcmp(peoples[i].name, peoples[j].name))
                m = 1;
        }
        if (m == 0)
            l  ;
        
        m = 0;
        
        s = s   strlen(peoples[i].surname);
    }
    
    for (i = 0; i < 9; i  )
    
        for (j = 0; j < 9; j  )   
         qsort(peoples, 10, sizeof(struct people), &compare_people_num);
        
        
    for (i = 0; i < 10; i  )
    {
        printf("%s ", peoples[i].num);
        printf("%s ", peoples[i].name);
        printf("%s ", peoples[i].surname);
        printf("\n");
    }
    
    printf("\nYou have %d different names\n", l);
    printf("Avarege lenght of surname is = %f\n", s / 10);
}

int compare_people_num(const void *a, const void *b)
{
    const struct people *p1 = a;
    const struct people *p2 = b;

    return p1->num - p2->num;  
} 

I have written something like that. Sorry, maybe I am stupid a little bit, but I still don't understand (

  • Related