Thread: Cycle for looping

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

    Cycle for looping

    Hi, I have the following assignment:
    I have to do a program that asks the name of the alumn, and then input 3 grades.
    Once that is done, the program must calculate the average of each alumn and the average of each test.

    I have done some code, it works, but the average calculation doesn't work.
    Could you fix that for me please?
    Kindly post the whole code fixed, and if you want, an explanation, so I can understand better.
    Thanks :)

    Code:
    #include <stdio.h> int main ()
    {
      char matrice[7][8];
      int i, n;
      int k;
      int m;
      n = 5;
      m = 3;
      int mat1[n][m];
      int vet1[n];
      int somma = 0;
      int media;
    
      for (i = 1; i <= n; i++) {
        printf("\ninput name(%d): ", i);
        scanf("%s", matrice);
    
        printf("\n\n");
      }
    
      for (i = 1; i <= n; i++) {
        printf("\nName %d: %s ", i, matrice);
    
        for (k = 1; k <= m; k++) {
          printf("Input grades (%d)", k);
    
          scanf("%d", &mat1[k]);
        }
      }
    
      for (i = 1; i <= n; i++) {
        printf("\nName %d: %s ", i, matrice);
    
        printf("\n");
        for (k = 1; k <= m; k++)
          printf(" %d", mat1[k]);
      }
    
      //below does not work
      for (i = 0; i < n; i++) {
        for (i = 0; i < m; i++) {
          somma = somma + matrice[k];
        }
      }
      media = somma / (n * m);
      printf("\nAverage:  = %s", media);
    
      printf("\n\n");
      getc;
    }
    Somma = Sum
    Media = Average
    Matrice = Matrix
    Last edited by Salem; 03-22-2020 at 10:15 PM. Reason: removed crayola

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by d03nvc9
    I have done some code, it works, but the average calculation doesn't work.
    How does it not work? For example, what is your test input, expected output, and actual output? How did you arrive at the expected output from the test input (i.e., verify that you own calculations are correct)?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like I said in your previous thread.

    Fix your warnings.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:17:11: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[8]’ [-Wformat=]
         scanf("%s", matrice);
               ^
    bar.c:23:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[8]’ [-Wformat=]
         printf("\nName %d: %s ", i, matrice);
                ^
    bar.c:28:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[(sizetype)(m)]’ [-Wformat=]
           scanf("%d", &mat1[k]);
                 ^
    bar.c:33:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[8]’ [-Wformat=]
         printf("\nName %d: %s ", i, matrice);
                ^
    bar.c:37:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
           printf(" %d", mat1[k]);
                  ^
    bar.c:43:13: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
           somma = somma + matrice[k];
                 ^
    bar.c:47:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
       printf("\nAverage:  = %s", media);
              ^
    bar.c:50:3: warning: statement with no effect [-Wunused-value]
       getc;
       ^
    bar.c:11:7: warning: unused variable ‘vet1’ [-Wunused-variable]
       int vet1[n];
           ^
    > for (i = 1; i <= n; i++)
    Arrays start at 0, not 1.
    For an n element array, it's for (i = 0; i < n; 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.

  4. #4
    Registered User
    Join Date
    Feb 2020
    Posts
    7
    Quote Originally Posted by laserlight View Post
    How does it not work? For example, what is your test input, expected output, and actual output? How did you arrive at the expected output from the test input (i.e., verify that you own calculations are correct)?
    5 names in input
    John, Patrick, Ben, Marck, Andrew

    And 3 grades for each person
    6, 5, 8
    9, 8, 6
    6, 9, 9
    7, 6, 7
    5, 4, 7

    And expect in output the average of each test, and for each student

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by d03nvc9 View Post
    5 names in input
    John, Patrick, Ben, Marck, Andrew

    And 3 grades for each person
    6, 5, 8
    9, 8, 6
    6, 9, 9
    7, 6, 7
    5, 4, 7

    And expect in output the average of each test, and for each student
    expected output, and actual output?
    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

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
      //below does not work
      for (i = 0; i < n; i++) {
        for (i = 0; i < m; i++) {
          somma = somma + matrice[k];
        }
      }
    Where does the value of k change in this loop code?
    What is the starting value of k ?

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FOR cycle problem
    By gyilki in forum C Programming
    Replies: 13
    Last Post: 11-14-2018, 06:46 AM
  2. Graph (contains cycle)
    By Siggi in forum C Programming
    Replies: 7
    Last Post: 05-08-2015, 01:40 PM
  3. Problem with while cycle
    By Lima in forum C Programming
    Replies: 5
    Last Post: 10-29-2010, 09:57 AM
  4. Cycle
    By Gordon in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2007, 10:58 AM
  5. How to Cycle Through Structs
    By Grantyt3 in forum C++ Programming
    Replies: 13
    Last Post: 08-27-2005, 04:29 PM

Tags for this Thread