T table of n rows and m columns

I'm supposed to create a program that allows adding the number of positive elements and the negatives of a T table of n rows and m columns.

I managed to do fine until the mention of a T table. Is it possible to do it? If so, how? Or am I overlooking this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>

int main()
{
	int matrix[10][10], m, n, row, col, pos=0, neg=0;
	
	printf("Enter number of rows: "); 
	scanf("%d", &row); 
	printf("Enter number of columns: "); 
	scanf("%d", &col); 
    
	printf("\nEnter values to the matrix: \n");
	for(n=1; n<=row ; n++)
	{
        for(m=1; m<=col ; m++)
        {
			printf("\nEnter matrix[%d][%d] value: ", n, m);
			scanf("%d", &matrix[n][m]);
        }
    }
    
    printf("\n");
    
    for(n=1; n<=row ; n++)
	{
        for(m=1; m<=col ; m++)
        {
			if(matrix[n][m]>0)
			{
				pos = pos + 1;
			}
			else if(matrix[n][m]<0)
			{
				neg = neg + 1;
			}
        }
    }
    
    printf("Total sum of positives: %d \n", pos);
    printf("Total sum of negatives: %d \n", neg);
}
Last edited on
@SilvaLay

Arrays start with 0, not 1. Your for loops go from 1 to, and include, row and col. But, if the user enters a number 10 or higher for the value of row or col, you will be going out of bounds on the array. You should not allow the user to enter a number larger than the array size.
dynamic allocation is the only way in C to produce a user-defined table. You can either do that, or make 'matrix' bigger than the user should ever need, like 100 X 100 (no one is going to type that much stuff into this program). If you have not covered pointers yet, that is probably what is expected.
I usually try my best to keep things at minimal when I'm testing it out, then I change it when done. And yes I am aware of the for loop, I recently changed that.
"T" is usually type, like int, double, string, any class, etc. If you don't have any particular instructions, then it's probably okay to choose some arbitrary type (you chose to make a matrix of integers). Consider doing all this in C++ instead of C; would make a lot of things easier.
Topic archived. No new replies allowed.