Thread: What is wrong with the code? How the element values of array changes?

  1. #1
    Registered User
    Join Date
    Sep 2019
    Posts
    2

    Exclamation What is wrong with the code? How the element values of array changes?

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i,j,row=1,col=3,sum=0;
    	int a[row][col];
    	printf("Enter the elements on by one:");
    	for(i=1;i<=row;i++)
    	{
    		for(j=1;j<=col;j++)
    		{
    			scanf("%d",&a[i][j]);						
    		}
    	}
    	
    	printf("The A matrix \n");
    	for(i=1;i<=row;i++)
    	{
    		for(j=1;j<=col;j++)
    		{
    			printf("%5d",a[i][j]);			
    		}
    		printf("\n");
    	}
    	int colsum[col];	//To calculate column sum
    	for(j=1;j<=col;j++)
    	{
    		colsum[j]=0;
    		for(i=1;i<=row;i++)
    		{
    			colsum[j] = colsum[j] + a[i][j];
    		}
    	}
    	printf("The A matrix \n");
    	for(i=1;i<=row;i++)
    	{
    		for(j=1;j<=col;j++)
    		{
    			printf("%5d",a[i][j]);			
    		}
    		printf("\n");
    	}
    	printf("\n The Column Sum is =");
    	for(i=1;i<=col;i++)
    		{
    			printf("%5d",colsum[i]);			
    		}
    	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,659
    > for(i=1;i<=row;i++)
    > for(j=1;j<=col;j++)
    Arrays start at subscript 0, not 1.

    for ( i = 0 ; i < N ; i++ )
    is the idiomatic loop for iterating over an array of length N.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2019
    Posts
    2
    Agreed, subscript start at 0, but starting at 1 is not invalid.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Agreed, subscript start at 0, but starting at 1 is not invalid.
    True, if you don't ever want to use the element at index 0. On the other hand, accessing a[row][col] when you declared a as int a[row][col] is invalid as you're accessing the array out of bounds.

    So you either want to start from 0, or you want to declare a as int a[row + 1][col + 1].
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-12-2016, 06:06 AM
  2. Replies: 2
    Last Post: 01-05-2016, 12:22 PM
  3. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  4. Replies: 2
    Last Post: 08-12-2012, 08:34 PM
  5. Replies: 8
    Last Post: 04-23-2007, 09:22 AM

Tags for this Thread