Thread: View input and sum rows and columns

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    7

    Exclamation View input and sum rows and columns

    Hi, I need to fix a program with a main menu that prints the input inserted, sums the rows and sums the columns.
    I made something up but it doesn't work and I can't understand why.

    Here are the problems:
    The function 2 prints random numbers in a loop.
    The function 3 doesn't print anything
    The function 4 prints just "0".

    Code:
    #include<stdio.h>
    #include<string.h>
    int main(void)
    {
      int selection;
      int matrice[30][30];
      int i;
      int j;
      int m;
      int n;
      int MAXn;
      int vet;
     
      do {
        printf
            ("+----------------------------------------------------------------------------------------------+\n");
        printf("1) input \n");
        printf("2) view input \n3) sum rows\n4) sum colums \n9) exit\n--->");
        scanf("%d", &selection);
        if (selection == 1) {
          printf("how many numbers will be inserted? ");
          scanf("%d", &n);
     
     
          printf("input a sequence of %d numbers\n", n);
          for (i = 0; i < n; i++) {
     
            for (j = 0; j < n; j++) {
              printf("element %d: ", i + 1);
              scanf("%d", &matrice[j]);
            }
            printf("\n");
          }
     
        }
        if (selection == 2) {
          printf("the sequence is the following:\n");
          for (i = 0; i < n; i++)
            for (j = 0; i < n; j++)
              printf("element %d: %d\n", i + 1, matrice[j]);
          printf("\n");
        }
        if (selection == 3) {
          int somma;
     
          for (i = 1; i <= n; i++) {
            int somma = 0;
            for (j = 1; i <= m; i++) {
              somma = somma + matrice[j];
              printf("sum: %d", &matrice[j]);
            }
            printf("sum: %d", somma);
          }
     
     
        }
     
        if (selection == 4) {
          int somma;
          int righe;
          int colonne;
          int a[10][10];
     
          for (righe = 0; righe < i; righe++) {
            somma = 0;
            for (colonne = 0; colonne < j; colonne++) {
              somma = somma + a[colonne][righe];
            }
            printf("The sum of Column Elements in a Matrix =  %d \n", somma);
          }
     
        }
     
        if (selection > 10)         // errore
        {
          printf("your choice is not valid");
        }
        if (selection == 9) {
          printf("Exiting...");
          getc;
        }
      }
      while (selection != 9);
     
     
     
    
    }

    Last edited by d03nvc9; 02-25-2020 at 03:04 AM. Reason: Mistyping

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,864
    Use meaningful variable names instead of i, m, and n.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,057
    There are many bad use of printf/scanf calls, and many unused or uninitialised variables.

    Code:
    $ gcc -Wall -Wextra -O2 foo.c
    foo.c: In function ‘main’:
    foo.c:30:17: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[30]’ [-Wformat=]
               scanf("%d", &matrice[j]);
                     ^
    foo.c:40:18: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]
               printf("element %d: %d\n", i + 1, matrice[j]);
                      ^
    foo.c:49:17: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
               somma = somma + matrice[j];
                     ^
    foo.c:50:18: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int (*)[30]’ [-Wformat=]
               printf("sum: %d", &matrice[j]);
                      ^
    foo.c:44:11: warning: unused variable ‘somma’ [-Wunused-variable]
           int somma;
               ^
    foo.c:80:7: warning: statement with no effect [-Wunused-value]
           getc;
           ^
    foo.c:12:7: warning: unused variable ‘vet’ [-Wunused-variable]
       int vet;
           ^
    foo.c:11:7: warning: unused variable ‘MAXn’ [-Wunused-variable]
       int MAXn;
           ^
    foo.c:19:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &selection);
         ^
    foo.c:22:7: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
           scanf("%d", &n);
           ^
    foo.c:30:11: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
               scanf("%d", &matrice[j]);
               ^
    foo.c:48:9: warning: ‘m’ may be used uninitialized in this function [-Wmaybe-uninitialized]
             for (j = 1; i <= m; i++) {
             ^
    foo.c:8:7: warning: ‘j’ may be used uninitialized in this function [-Wmaybe-uninitialized]
       int j;
           ^
    foo.c:7:7: warning: ‘i’ may be used uninitialized in this function [-Wmaybe-uninitialized]
       int i;
           ^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get rows in a matrix when only columns are given
    By torquemada in forum C Programming
    Replies: 11
    Last Post: 09-13-2011, 07:57 PM
  2. How to read columns and rows
    By university in forum C Programming
    Replies: 1
    Last Post: 12-05-2010, 12:09 PM
  3. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  4. printing rows and columns
    By datainjector in forum C# Programming
    Replies: 1
    Last Post: 08-03-2003, 05:42 PM
  5. Adding Columns and Rows..Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 03:52 PM

Tags for this Thread