0

The code I have written is not giving the input that are the marks been provided to it by the user , insted of that it is printing all random garbage values. Kindly help to solve the problem.

#include <stdio.h>

void display(int number_1, int number_2)
{
    int marks[number_1][number_2];

    for (int i = 0; i < number_1; i++)
    {
        for (int j = 0; j < number_2; j++)
        {
            printf("The marks of student %d in subject %d is: %d\n", i + 1, j + 1, marks[i][j]);
        }
    }
}

int main()
{
    int a;
    printf("The number of student:\n");
    scanf("%d", &a);
    int b;
    printf("The subject:\n");
    scanf("%d", &b);
    int i;
    int j;
    int marks[i][j];

    for (i = 0; i < a; i++)
    {
        for (j = 0; j < b; j++)
        {
            printf("Enter The marks of student %d in subject %d : \n", i + 1, j + 1);
            scanf("%d", &marks[i][j]);
        }
    }

    display(a, b);

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Prerna
  • 17
  • 3
  • 2
    `int i; int j; int marks[i][j];` What are the values of `i` and `j` here? You probably meant `int marks[a][b];` – 001 Feb 04 '22 at 21:49
  • 2
    Also note the `marks` array in `main` is not the same one as the one in `display`. You need to pass it from `main` to `display`. – 001 Feb 04 '22 at 21:52

3 Answers3

1

This declaration of the array in the file scope

int marks[number_1][number_2];

is invalid and does not make a sense. Remove it.

In main the number of elements in the array are specified with variables a and b.

So you need to write

    int marks[a][ b];

instead of

    int marks[i][ j];

To call the function for the array marks you need to declare a function parameter that will accept the array.

The function declaration can look like

void display( int number_1, int number_2, int marks[number_1][number_2] )
{
    for (int i = 0; i < number_1; i++)
    {
        for (int j = 0; j < number_2; j++)
        {
            printf("The marks of student %d in subject %d is: %d\n", i + 1, j + 1, marks[i][j]);
        }
    }
}

and in main the function is called like

display( a, b, marks );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
#include <stdio.h>
int display(int students, int subjects, int marks[students][subjects])
{
    for (int i = 0; i < students; i++)
    {
        for (int j = 0; j < subjects; j++)
        {
            printf("The marks of student %d in subject %d is %d\n", i + 1, j + 1, marks[i][j]);
        }
    }
}

int main()
{
    int m = 2;
    int n = 3;
    int marks[2][3];
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("Enter the marks of student %d in subject %d\n", i + 1, j + 1);
            scanf("%d", &marks[i][j]);
        }
    }
    display(m, n, marks);
    return 0;
}
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 23 '22 at 16:35
0

Create a 2-d array by taking input from the user. Write a display function to print the content of this 2-d array enter image description here

  • Please add code and data as text ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – Adrian Mole Apr 13 '23 at 08:52
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 13 '23 at 08:53