Thread: Additon in Array for specific column

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    6

    Additon in Array for specific column

    Hi.. i want modify my programme 2 dimensional which only calculate addition only in cloumn0. Currently my programme calculate all row and column but dont want that. Anyone please advice how i cant modify. Tq here is my code.
    Code:
     #include <stdio.h>
    
    int main() {
     int marks[4][4]  ;
    int  j ; 
    int i;
    int sum = 0;
    int average= 0;
     
    printf("Enter the marks of 4 students :- \n");
    
      for ( j = 0; j < 4 ; j++ ) 
      for(i=0; i<4; i++)
      
      {
     
         printf("Marks of student [%d] [%d] : ",j,i );
     
         scanf("%d", &marks[j][i]);
          }
                 for(j= 0;j<4;j++) 
                 for(i=0; i<4; i++)
          {
     
      sum = sum+marks[j][i];} 
      average =  sum/4;
      printf("Sum Of Column1: %d\n", sum); 
      printf("Average Of Column1: %d\n", average);
      return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    The first step is to indent your code properly, so you (and everyone else) can see the program flow at a glance.
    Code:
    #include <stdio.h>
    
    int main()
    {
        int marks[4][4];
        int j;
        int i;
        int sum = 0;
        int average = 0;
    
        printf("Enter the marks of 4 students :- \n");
        for (j = 0; j < 4; j++)
            for (i = 0; i < 4; i++)
            {
                printf("Marks of student [%d] [%d] : ", j, i);
                scanf("%d", &marks[j][i]);
            }
    
        for (j = 0; j < 4; j++)
            for (i = 0; i < 4; i++) {
                sum = sum + marks[j][i];
            }
        average = sum / 4;
        printf("Sum Of Column1: %d\n", sum);
        printf("Average Of Column1: %d\n", average);
        return 0;
    }
    If you only want to sum column 0, then remove the inner loop and sum marks[j][0].
    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. sorting a 2d array by column
    By antros48 in forum C Programming
    Replies: 4
    Last Post: 10-24-2013, 09:11 AM
  2. Subtracting one column in a 2d array from another?
    By zaihed13 in forum C Programming
    Replies: 6
    Last Post: 10-19-2013, 09:29 AM
  3. Adding a new column in array
    By Tomislav Pti in forum C Programming
    Replies: 3
    Last Post: 11-22-2012, 04:48 AM
  4. adding up array column total
    By dmckay in forum C Programming
    Replies: 1
    Last Post: 01-25-2010, 03:14 PM
  5. additon of 2 matrices error in code
    By sekharsomu in forum C Programming
    Replies: 3
    Last Post: 08-09-2008, 02:36 PM

Tags for this Thread