Thread: Need help with simple c project-as as possible

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Need help with simple c project-as as possible

    Hello,

    Been working on this simple project in C but having issues with errors. I am new to programming so I'm having issues getting anywhere. Any hep is appreciated. Thanks.

    Project Info:

    Write a program that asks the user to enter number 1-16 in any order, and then display those
    numbers in a 4*4 arrangement. At last, it calculates the sums of each row, column and both diagonals.
    You can use 16 variables to store all the input value. No need to check if the input is valid.

    My Code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
    
    
    void main() 
    { 
    int num[4][4]; 
    int i,j,sum; 
    printf("\nEnter any 16 numbers :"); 
    for(i=0;i<4;i++) 
    { for(j=0;j<4;j++) 
    { scanf("%d",&num[i][j]); } } 
    printf("\n Your 16 entered no are as follows \n"); 
    for(i=0;i<4;i++) { 
    for(j=0;j<4;j++) { 
    printf("%d \t",num[i][j]); } 
    printf("\n"); } 
    for(i=0;i<4;i++) { sum=0; 
    for(j=0;j<4;j++) { sum=sum+num[i][j]; } 
    printf("\nSum of row %d is : %d ",i+1,sum); } 
    for(i=0;i<4;i++) { sum=0; 
    for(j=0;j<4;j++) { sum=sum+num[j][i]; } 
    printf("\nSum of coloumn %d is : %d",i+1,sum); } 
    printf("\nSum of Diagonal 1 : %d ",num[0][0]+num[1][1]+num[2][2]+num[3][3]); 
    printf("\nSum of Diagonal 2 : %d ",num[0][3]+num[1][2]+num[2][1]+num[3][0]); getch(); }
    
    
    system("pause");
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Please indent your code!!! It not only makes things more pleasant for people on here trying to help, but it would have lead you to your problem (assuming it's the same compile error I see):

    Code:
    temp.c(30): error C2143: syntax error : missing ')' before 'string'
    temp.c(30): error C2143: syntax error : missing '{' before 'string'
    temp.c(30): error C2059: syntax error : '<Unknown>'
    temp.c(30): error C2059: syntax error : ')'
    temp.c(33): error C2059: syntax error : 'return'
    temp.c(34): error C2059: syntax error : '}'

    Now, had you nicely indented your code.......

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <math.h>
     
     
    void main() 
    { 
    	int num[4][4]; 
    	int i,j,sum; 
    	printf("\nEnter any 16 numbers :"); 
    	for(i=0;i<4;i++) 
    	{ 
    		for(j=0;j<4;j++) 
    		{ 
    			scanf("%d",&num[i][j]); 
    		} 
    	} 
    	printf("\n Your 16 entered no are as follows \n"); 
    	for(i=0;i<4;i++) 
    	{ 
    		for(j=0;j<4;j++) { 
    			printf("%d \t",num[i][j]); 
    		} 
    			printf("\n"); 
    	} 
    	for(i=0;i<4;i++) 
    	{ 
    		sum=0; 
    		for(j=0;j<4;j++) 
    		{ 
    			sum=sum+num[i][j]; 
    		} 
    	printf("\nSum of row %d is : %d ",i+1,sum); 
    	} 
    	for(i=0;i<4;i++) 
    	{ 
    		sum=0; 
    		for(j=0;j<4;j++) 
    		{ 
    			sum=sum+num[j][i]; 
    		} 
    		printf("\nSum of coloumn %d is : %d",i+1,sum); 
    	} 
    	printf("\nSum of Diagonal 1 : %d ",num[0][0]+num[1][1]+num[2][2]+num[3][3]); 
    	printf("\nSum of Diagonal 2 : %d ",num[0][3]+num[1][2]+num[2][1]+num[3][0]); getch(); 
    }
    
    system("pause"); 
    return 0;
    }
    You can easily see that you have mismatched curly braces, so you have some statements outside of any functions.

    Indentation is really important!!

    Haven't tried to see if your program actually works yet, looks okay to me.

    Code:
    void main()
    Should be int main(void), see:
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Nevermind thanks. It works. Visual Studio was just being not cool
    Last edited by vlador3; 10-19-2012 at 08:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with file input on simple C++ Project
    By Trooper88 in forum C++ Programming
    Replies: 2
    Last Post: 12-08-2010, 01:11 PM
  2. Help with a simple Terminal Project
    By Sretsam in forum C Programming
    Replies: 12
    Last Post: 09-19-2010, 05:36 PM
  3. Can't make a simple Project work
    By kantze in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2007, 03:21 AM
  4. Simple Bank Account Uni Project Help!!!
    By griff in forum C Programming
    Replies: 3
    Last Post: 05-09-2003, 05:49 PM
  5. Simple project becomes very annoying.
    By ... in forum C++ Programming
    Replies: 6
    Last Post: 02-27-2002, 11:42 PM