Search code examples
cpointersstructuredeclare

Confusion with Structures and how to store values with pointer in C


This question may be annoying, but I am trying to write code the way that I can most easily mentally process it and at this point it is without calling functions. I am rewriting my professors example code into my own, but am having a problem with what to store it into? He uses a pointer, but I just don't know how/where to declare it.

My apologies if this question is incorrectly worded. I am one of those people who just doesn't 'get it' when it comes to programming unfortunately.

My Professors:

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

/* Program that computes the average salary of the employees with an age equal or greater
than x, x is passed as a parameter to the main function. Employee is a structure with two 
fields: age and salary. */

struct employee {
   int age;
   float salary;
};

void readValues(struct employee *EMP, int n ) {
    int i;

    for(i=0; i<n; i++) {
        printf("Employee %i\n", i+1);
        printf("\tAge: ");
        scanf("%i", &EMP[i].age);
        printf("\tSalary: ");
        scanf("%f", &EMP[i].salary);
    }
}

float getAvg(struct employee *EMP, int minAge, int n ) {
    int i, nE = 0;
    float sum=0;

    for(i=0; i<n; i++) {
        if(EMP[i].age >= minAge) {
            sum = sum + EMP[i].salary;
            nE = nE + 1;
        }
    }   
    return sum/nE;
}

int main(int argc, char *argv[]) {
    int x, n;
    struct employee E[100]; 

    if (argc<3) {
        printf("Error: Some parameters missing.\n");
        return -1;
    }
    x = atoi(argv[1]);
    n = atoi(argv[2]);

    readValues(E, n);
    float avg = getAvg(E, x, n);
    printf("Avg. Salary = %.2f\n", avg);

     return 0;
}

My attempt:

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

#define minage 20

 struct employee{
    int age;
    float salary;
    };

int main()

{
    struct employee emp = {0};

    int i, n, numb=0, sum=0, avg;

    printf("How many employees do you have today?\n");
    scanf("%i", n);

    for(i=0; i<n; i++)
    {
        printf("Employee %i", i+1);
        printf("Age:\n");
        scanf("%i", &emp[i].age);
        printf("Salary:\n");
        scanf("%f", &emp[i].salary);
    }

    if (emp[i].age >= minage)
    {
        for(i=0, i<n; i++){
            sum = sum + emp[i].salary
            numb = numb + 1

        }
    }
    avg = sum / numb;

    printf("The average salary is %i\n", avg);

}

Solution

  • Refer the below Code: Comments Inline

    #include <stdio.h>
    #include <stdlib.h>
    
    #define minage 20
    
    struct employee{
        int age;
        float salary;
    };
    
    int main()
    {
        /* Use array of struct to store the employee record. */
        struct employee emp[100];
        int i, n, numb=0, sum=0;
        float avg = 0;       /* If avg is of type int then you will loose the precision when finding the average */
    
        printf("How many employees do you have today?\n");
        scanf("%i",&n); /* Use `&` when using scanf to read value into integer*/
    
        for (i = 0; i < n; i++) {  
            printf("Employee %i", i+1);
            printf("Age:\n");
            scanf("%i", &emp[i].age);
            printf("Salary:\n");
            scanf("%f", &emp[i].salary);
        }
    
        /* Your professor is calculating avg salary only if age >= minage.
         * But in your code your are trying to check if the last employee age is 
         * >= minage only after which calculating the avg salary of all employee's*/
        for (i = 0; i < n; i++) {
            if (emp[i].age >= minage) {  
                sum = sum + emp[i].salary;
                numb = numb + 1;
            }
        }
    
        /* Make sure to check whether numb != 0, else divide by zero exception
         * when there are no employee's with age greater than 20 (minage) */
        if (numb)
            avg = sum / numb;
    
        printf("The average salary is %.2f\n", avg);
        /* return 0; when return type of main() is int.
         * If not required then change to void main()*/
        return 0;
    }
    

    Also as your professor is initializing minage from user input. In your code it is hard coded to a fixed number 20.

    Try to read the minage from user as you did to get the number of employee's.