0

I have written above program in CODEBLOCKS to input 20 names into an array and input 20 marks into another array. In this program, I want to calculate the average, calculate the highest mark and check the name who has the highest mark. I tried above program and it compiled without error. But when I check the average, it gives full value.

For example: if the correct average is 48.95, program outputs 49 as average.

So, I want to solve that error and need the average in 2 decimal points.(%.00f) and also the printing of the name doesn't work. Can you help?

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

int main()
{
int grades[20];
char names[20];
int a,b,c=1,d=1,e,f,high,g=0,loc;
float avg,tot=0;
for(a=0;a<20;a++)
    {
    printf("Input Name %d : ",c);
    scanf("%s",&names[a]);
    c++;
    }
for(b=0;b<20;b++)
    {
    printf("Input Marks %d : ",d);
    scanf("%d",&e);

     if(e>=0&&e<=100)
        {
            grades[b]=e;
            d++;
        }
    else
        {
            printf("OUT OF RANGE.PLEASE INPUT A VALID NUMBER.\n");
            b--;
        }
    }
for(f=0;f<20;f++)
    {
        tot=tot+grades[f];
    }
printf("Total is %.00f\n",tot);
avg=tot/20.00;
printf("Average is %.00f\n",avg);
high=grades[0];
for(g=0;g<20;g++)
{
    if(high<grades[g])
        {
            high=grades[g];
            loc=g;
        }
}
printf("The average is %.00f\nThe highest grade is %d\nThe name of the person who has the highest grade is :%s",avg,high,names[loc]);
printf("The average is %.00f\n",avg);
}
3
  • 2
    scanf("%s",&names[a])is completely wrong. names is an array of chars, not an array of strings! Commented Dec 28, 2013 at 15:12
  • so how to solve that error? using %c,instead of %s? Commented Dec 28, 2013 at 15:24
  • @DilankaMadhawa I have edited my answer to fix scanf(..name) problem. Commented Dec 28, 2013 at 15:32

3 Answers 3

3

The conversion specification %.00f requests 0 digits after the decimal point, so that's what you get. If you want 2 digits after the decimal point, then use %.2f (or maybe %6.2f).

To fix the point that Matteo Pacini made in his comment, and deal with miscellaneous other issues too, you could use a variant of your code similar to this:

#include <stdio.h>

int main(void)
{
    int grades[20];
    char names[20][20];
    int a, b, f, high, g, loc;
    float avg, tot = 0;
    for (a = 0; a < 20; a++)
    {
        printf("Input Name %d: ", a+1);
        if (scanf("%19s", names[a]) != 1)
            return 1;
    }
    for (b = 0; b < 20; b++)
    {
        int e;
        printf("Input Marks %d: ", b+1);
        if (scanf("%d", &e) != 1)
            return 1;

        if (e >= 0 && e <= 100)
            grades[b] = e;
        else
        {
            printf("OUT OF RANGE.PLEASE INPUT A VALID NUMBER.\n");
            b--;
        }
    }
    putchar('\n');

    for (a = 0; a < 20; a++)
        printf("%-20s %3d\n", names[a], grades[a]);

    for (f = 0; f < 20; f++)
        tot = tot + grades[f];

    printf("Total is %6.2f\n", tot);
    avg = tot / 20.00;
    printf("Average is %6.2f\n", avg);

    high = grades[0];
    loc = 0;
    for (g = 0; g < 20; g++)
    {
        if (high < grades[g])
        {
            high = grades[g];
            loc = g;
        }
    }

    printf("The average is %6.2f\n", avg);
    printf("The highest grade is %d\n", high);
    printf("The name of the person who has the highest grade is: %s\n", names[loc]);

    return 0;
}

Given sample input data like this (file ga.marks), where the data was generated by two programs:

Student-01
Student-02
Student-03
Student-04
Student-05
Student-06
Student-07
Student-08
Student-09
Student-10
Student-11
Student-12
Student-13
Student-14
Student-15
Student-16
Student-17
Student-18
Student-19
Student-20
49
27
47
46
33
84
63
51
61
91
82
60
39
57
65
60
19
88
47
61

This is the output from running the program (named ga) as: ga < ga.marks (because I am not going to sit around typing 20 names and 20 numbers as input to a program):

Input Name 1: Input Name 2: Input Name 3: Input Name 4: Input Name 5: Input Name 6: Input Name 7: Input Name 8: Input Name 9: Input Name 10: Input Name 11: Input Name 12: Input Name 13: Input Name 14: Input Name 15: Input Name 16: Input Name 17: Input Name 18: Input Name 19: Input Name 20: Input Marks 1: Input Marks 2: Input Marks 3: Input Marks 4: Input Marks 5: Input Marks 6: Input Marks 7: Input Marks 8: Input Marks 9: Input Marks 10: Input Marks 11: Input Marks 12: Input Marks 13: Input Marks 14: Input Marks 15: Input Marks 16: Input Marks 17: Input Marks 18: Input Marks 19: Input Marks 20: 
Student-01            49
Student-02            27
Student-03            47
Student-04            46
Student-05            33
Student-06            84
Student-07            63
Student-08            51
Student-09            61
Student-10            91
Student-11            82
Student-12            60
Student-13            39
Student-14            57
Student-15            65
Student-16            60
Student-17            19
Student-18            88
Student-19            47
Student-20            61
Total is 1130.00
Average is  56.50
The average is  56.50
The highest grade is 91
The name of the person who has the highest grade is: Student-10

Note that the prompts are counter-productive for input from file. The code echoes its input at the end of the input. It does the calculations etc. I didn't bother to tune the data to produce a mean score of 48.95.

Sign up to request clarification or add additional context in comments.

2 Comments

in final line, "printf("The average is %.2f\nThe highest grade is %d\nThe name of the person who has the highest grade is :%s",avg,high,names[loc]);" isn't working bro
Dunno why not; it works for me in my code (which avoids massively long strings by splitting that printf() into 3 separate calls, and ensures the last line ends with a newline too).
1

Use printf("%.2f",avg) for 2 digits after the decimal point. You are using printf("%.00f",avg) which means 0 digits after the decimal point.

Comments

1

Your calculations are correct. Just the printing format is messed up.

Use the code below rather than %.00f:

 %.2f

To fix your name problem you have many ways. One possibility is as below:

char names[20][50];

This means you have 20 names with each length 50 max.

scanf("%s", names[a]);

Also you can change your scanf as above. Remove &.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.