brian
brian

Reputation: 57

Is it possible to input 2 data types in a 2d array?

So I was asked to write a program that takes a list of students’ names and marks and calculates the average marks. You are required to declare two arrays called names and marks. Assume the number of students are 5.

Declare array names: (use 2-D array) char names[num_std][name_len]; //name length can be 20 characters long

Declare array marks: float marks[num_std];

What annoyed me is I don't know why I get a null when I try to run the loop which calls the name and the marks. And I also wonder that is it a correct way to code my 2-d array in such a way?. sry, that I'm still new with 2-d array and it confused me .

Can someone code me a complete code as I want to refer where I'm wrong at. thx a lot :D

///////////////////////code////////////////////////

#include <stdio.h>

int main ()
{
    int n,i;
    char num_std;
    char name_len[20];
    char name[num_std][name_len[20]];
    float sum = 0,marks [5];
    for (n=0;n<5;n++)
    {
        printf("\nEnter student name:");
        scanf("%s",&name[n][1]);

        printf("Enter student marks:");
        scanf("%f",&marks[n]);
        sum += marks[n];
    }

        for(n=1;n<5;n++)
        {
        printf("%s %f",name[n][1],marks[n]);
         }

    float average = sum/5.0;
    printf("\nAverage mark of 5 students = %.2f",average);
    
    return 0;
    }

////////////////////output///////////////////////

Enter student name:Brian
Enter student marks:99

Enter student name:Edward
Enter student marks:79

Enter student name:Fred
Enter student marks:69

Enter student name:Adrian
Enter student marks:45

Enter student name:Smith
Enter student marks:55
(null) 45.000000
Average mark of 5 students = 69.40
Process returned 35 (0x23)   execution time : 40.064 s
Press any key to continue.

Upvotes: 0

Views: 794

Answers (3)

MED LDN
MED LDN

Reputation: 669

It's good technique to use i in the foor loop

#include <stdio.h>

int main ()
{
char name[5][20];
float marks [5],sum=0;
for (int i=0;i<5;i++)
{
    printf("\nEnter student name :");
    scanf("%s[^\n]",name[i]);
    printf("Enter student marks :");
    scanf("%f",&marks[i]);
    sum += marks[i];
}

    for(int i=0;i<5;i++)
    {
          printf("\n%s : %f",name[i],marks[i]);
    }
  printf("\nAverage mark of 5 students = %.2f",(double)sum/5.0);
 //float average=(float) sum/5.0;
 //printf("\nAverage mark of 5 students = %.2f",average);
 return 0;
}

Upvotes: 0

Eric Postpischil
Eric Postpischil

Reputation: 223693

char name[num_std][name_len[20]]; is wrong. name_len is an array of 20 elements, so the indices for those elements range from 0 to 19. Accessing name_len[20] is incorrect. Further, no element in name_len has been given a value at this point. The C standard allows arrays with lengths set during program execution, but the length must be given when the array is defined. It cannot be a length that adjusts when a variable changes. At this point in your learning, change the length to the maximum you were given: char name[num_std][21];. 21 array elements (each a char) allows for 20 letters in a name plus a null character to mark the end. (Later you will learn other ways to manage multiple strings of varying lengths.)

You also need to give num_std a value before this definition. It is not clear what that value should be. Other parts of your program are hard-coded for five students. In that case, you can define num_std with char num_std = 5;. In assignments like this, it is common that the program would first read num_std from input. In that case, you need a scanf to read num_std before the line char name[num_std][21];.

scanf("%s",&name[n][1]); is wrong. We want to put the string at the start of the space for the name, and that space starts with name[n][0], not name[n][1]. So you can use &name[n][0]. The expression name[n] will have the same value as that, because, in C, arrays in expressions are automatically converted to pointers to their first elements (except when used as the operand of sizeof or unary & or as a string literal initializing an array).

In printf("%s %f",name[n][1],marks[n]);, you should also use name[n][0], and you again need to pass its address, &name[n][0] or name[n].

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

How your code is wrong:

#include <stdio.h>

int main ()
{
    /* i is declared but not used (legal) */
    int n,i;
    char num_std;
    char name_len[20];
    /* uninitialized variable num_std is used as array length (critical) */
    /* out-of-range name_len[20] is used (critical) */
    char name[num_std][name_len[20]];
    float sum = 0,marks [5];
    for (n=0;n<5;n++)
    {
        printf("\nEnter student name:");
        /* the first element of the array name[n] is not used (legal) */
        scanf("%s",&name[n][1]);

        printf("Enter student marks:");
        scanf("%f",&marks[n]);
        sum += marks[n];
    }

    /* indentation is broken (legal) */
        /* the first data is not printed (legal) */
        for(n=1;n<5;n++)
        {
        /* char (name[n][1]) is passed where char* is required (critical) */
        /* data is printed without being separated (legal) */
        printf("%s %f",name[n][1],marks[n]);
         }

    float average = sum/5.0;
    /* newline character is not printed at end of output (legal) */
    printf("\nAverage mark of 5 students = %.2f",average);
    
    return 0;
    /* but indentation is broken (legal) */
    }

Here "critical" means the mistake will invoke undefined behavior and "legal" means that the mistake won't invoke undefined behavior but the behavior may not be what is wanted or not typical.

Fixed code:

#include <stdio.h>

int main (void)
{
    int n;
    char name[5][21]; /* 20-character name + 1 terminating null-character */
    float sum = 0,marks [5];
    for (n=0;n<5;n++)
    {
        printf("\nEnter student name:");
        scanf("%20s",name[n]); /* specify length limit to avoid buffer overrun */

        printf("Enter student marks:");
        scanf("%f",&marks[n]);
        sum += marks[n];
    }

    for(n=0;n<5;n++)
    {
        printf("%s %f\n",name[n],marks[n]);
    }

    float average = sum/5.0;
    printf("\nAverage mark of 5 students = %.2f\n",average);
    
    return 0;
}

Upvotes: 1

Related Questions