10 Replies - 576 Views - Last Post: 15 February 2010 - 09:18 PM Rate Topic: -----

#1 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Help with array code errors

Posted 15 February 2010 - 01:14 PM

I have been working on creating a program to display total sales results for four sales people selling five products. I have worked hard on the code but I can't seem to quite get it to compile. I am specifically concerned with my arrays and believe I need some help making sure I use them and code them correctly. I don't get how to fix the code. Some help would be appreciated.
#include <stdio.h>
     

      int main()
  
      {
	
      const int PEOPLE = 5, PRODUCTS = 6;
 
      double sales[ PEOPLE ][ PRODUCTS ] = { 0.0 }, value;
  
      totalSales, productSales[ PRODUCTS ] = { 0.0 };
		int salesPerson, product;
     
	  int  i, j;
  
       
  
      printf("Enter the sales person (1 - 4), ");
  
      printf("product number (1 - 5)\nand total sales.");
  
     printf("Enter -1 for the sales person to end input.\n");
  
      scanf("%d", salesPerson);
  
       
  
      while ( salesPerson != -1 ) {
  
      scanf("%d" ,&product ;
	  scanf("%d", &value);
  
      sales[ salesPerson ][ product ] += value;
  
      scanf("%d", &salesPerson);
  
      }
  
       
  
      printf("\nThe total sales for each sales person ");
  
     printf("are displayed\nat the end of each row,");
  
    printf("and the total sales for each\nproduct ");
  
     printf("are displayed at the bottom of each column.\n");
  
         
  printf("%10d", 1);
  printf("%10d", 2);
  printf("%10d", 3);
  printf("%10d", 4);
  printf("%10d", 5);
  printf("%12d", "Total\n");
       
  
      for ( i = 1; i < PEOPLE; ++i ) {
  
      totalSales = 0.0;
		printf("%d", &i);		
  
       
  
      for ( j = 1; j < PRODUCTS; ++j ) {
  
      totalSales += sales[ i ][ j ];   
  
      printf("%.2f", sales [i][j]);
  
      productSales[ j ] += sales[ i ][ j ];   
  
      }
  
        printf("%.2f", sales[i][j]);
  
      }
  
       
  
     printf( "%.2f","\nTotal"productSales );

      
       
  
      for ( j = 2; j < PRODUCTS; ++j )
  
      printf("%.2f", productsales[j])
         
      
      return 0;
  
      }


Is This A Good Question/Topic? 0
  • +

Replies To: Help with array code errors

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Help with array code errors

Posted 15 February 2010 - 01:19 PM

You have a lot of syntax errors:
totalSales, productSales[ PRODUCTS ] = { 0.0 };


Those are undeclared identifiers. Did you mean:
double totalSales, productSales[ PRODUCTS ] = { 0.0 };


Also here:
scanf("%d" ,&product ;


Should be:
scanf("%d" ,&product);


Here you are missing a ',':
printf( "%.2f","\nTotal", productSales ); // added ,


Also, here:
 printf("%.2f", productsales[j])


Should be:
 printf("%.2f", productSales[j]);


This post has been edited by sarmanu: 15 February 2010 - 01:20 PM

Was This Post Helpful? 1
  • +
  • -

#3 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Help with array code errors

Posted 15 February 2010 - 01:50 PM

Thank you Sarmanu!
I have edited the code and it now compiles but stops after I enter the saleperson number. {lease help me out. Here is the code as it stands now.
#include <stdio.h>
     

      int main()
  
      {
	
      const int PEOPLE = 5, PRODUCTS = 6;
 
      double sales[ 5 ][ 6 ] = { 0.0 }, value;
  
      double totalSales, productSales[ 6 ] = { 0.0 };
		int salesPerson, product;
     
	  int  i, j;
  
       salesPerson = 0;
	   product =0;
	   value = 0;
  
	   printf("Enter the sales person (1 - 4) Enter -1 to end input:");
       
      scanf("%d", salesPerson);
      
      while ( salesPerson != -1 ) {
		  printf("Enter product number (1 - 5):\n");
      scanf("%d" ,&product) ;
	  printf("Enter total sales of product:\n");
	  scanf("%d", &value);
  
      sales[ salesPerson ][ product ] += value;
  
      scanf("%d", &salesPerson);
  
      }
  
       
  
      printf("\nThe total sales for each sales person ");
  
     printf("are displayed\nat the end of each row,");
  
    printf("and the total sales for each\nproduct ");
  
     printf("are displayed at the bottom of each column.\n");
  
         
  printf("%10d", 1);
  printf("%10d", 2);
  printf("%10d", 3);
  printf("%10d", 4);
  printf("%10d", 5);
  printf("%12d", "Total\n");
       
  
      for ( i = 1; i < PEOPLE; ++i ) {
  
      totalSales = 0.0;
		printf("%d", &i);		
  
       
  
      for ( j = 1; j < PRODUCTS; ++j ) {
  
      totalSales += sales[ i ][ j ];   
  
      printf("%.2f", sales [i][j]);
  
      productSales[ j ] += sales[ i ][ j ];   
  
      }
  
        printf("%.2f", sales[i][j]);
  
      }
  
       
  
     printf( "%.2f,","\nTotal",productSales );

      
       
  
      for ( j = 2; j < PRODUCTS; ++j )
  
      printf("%.2f", productSales[j]);
         
      
      return 0;
  
      }






View Postsarmanu, on 15 February 2010 - 12:19 PM, said:

You have a lot of syntax errors:
totalSales, productSales[ PRODUCTS ] = { 0.0 };


Those are undeclared identifiers. Did you mean:
double totalSales, productSales[ PRODUCTS ] = { 0.0 };


Also here:
scanf("%d" ,&product ;


Should be:
scanf("%d" ,&product);


Here you are missing a ',':
printf( "%.2f","\nTotal", productSales ); // added ,


Also, here:
 printf("%.2f", productsales[j])


Should be:
 printf("%.2f", productSales[j]);


Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Help with array code errors

Posted 15 February 2010 - 02:00 PM

scanf("%d", salesPerson);


Here you are missing an &:
scanf("%d", &salesPerson);


Should do the trick. Also, don't use & when printing values:
printf("%d", &i);  


should be:
printf("%d", i); 


We \want to print the data of the number, not it's address.
Was This Post Helpful? 1
  • +
  • -

#5 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Help with array code errors

Posted 15 February 2010 - 02:11 PM

View Postsarmanu, on 15 February 2010 - 01:00 PM, said:

scanf("%d", salesPerson);


Here you are missing an &:
scanf("%d", &salesPerson);


Should do the trick. Also, don't use & when printing values:
printf("%d", &i);  


should be:
printf("%d", i); 


We \want to print the data of the number, not it's address.


Thank you Sarmanu!! I am getting there now. It does allow me to enter the product number and sales amount. However, it just stops there. It does not prompt for next salesperson's data. Ideally I would like to update this to run for each salesperson 20 times so that data for twenty days is included in sales totals. Any ideas in how to go from here?
Was This Post Helpful? 0
  • +
  • -

#6 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Help with array code errors

Posted 15 February 2010 - 04:19 PM

OK I have gotten the program to the point that it allows me to enter the data I want to enter. However, I do not know how to get the program to loop back corectly and ask for salesPerson number and stop the loop when I enter -1.
Help!
#include <stdio.h>
     
      int main()
 {
	      const int PEOPLE = 5, PRODUCTS = 6;
          double sales[ 5 ][ 6 ] = { 0.0 }, value;
          double totalSales, productSales[ 6 ] = { 0.0 };
		  int salesPerson, product;
   	      int  i, j;
 
          salesPerson = 0;
	      product =0;
	      value = 0;

	       printf("Enter the sales person (1 - 4) Enter -1 to end input:");
       	   scanf("%d", &salesPerson);
             while ( salesPerson != -1 ) {
		         printf("Enter product number (1 - 5):\n");
                 scanf("%d" ,&product) ;
	             printf("Enter total sales of product:\n");
	             scanf("%d", &value);
			     sales[ salesPerson ][ product ] += value;
  			     
            }
       		     printf("\nThe total sales for each sales person ");
  			     printf("are displayed\nat the end of each row,");
  			     printf("and the total sales for each\nproduct ");
  			     printf("are displayed at the bottom of each column.\n");
  			     printf("%10d", 1);
			     printf("%10d", 2);
			     printf("%10d", 3);
			     printf("%10d", 4);
			     printf("%10d", 5);
			     printf("%12d", "Total\n");
                 for ( i = 1; i < PEOPLE; ++i ) {
                     totalSales = 0.0;
		             printf("%d", i);		
                     for ( j = 1; j < PRODUCTS; ++j ) {
  				         totalSales += sales[ i ][ j ];   
  				         printf("%.2f", sales [i][j]);
  				         productSales[ j ] += sales[ i ][ j ];   
                    }
		            printf("%.2f", sales[i][j]);
  
                }
                    printf( "%.2f,","\nTotal",productSales );

                        for ( j = 2; j < PRODUCTS; ++j )
                            printf("%.2f", productSales[j]);
              return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Help with array code errors

Posted 15 February 2010 - 06:02 PM

My code has been corrected to include a do while loop so that it loops correctly. I need to understand why the compiler is giving me an error on the first printf statement after the do while loop. It says "printf identifier" Here is my code so far...Thank you!!
#include <stdio.h>
     
      int main()
 {
      const int PEOPLE = 5, PRODUCTS = 6;
      double sales[ 5 ][ 6 ] = { 0.0 }, value;
      double totalSales, productSales[ 6 ] = { 0.0 };
      int salesPerson, product;
      int  i, j;
 
      salesPerson = 0;
      product =0;
      value = 0;
      
      do while ( salesPerson != -1 ) {
        printf("Enter the sales person (1 - 4) Enter -1 to end       input:");
        scanf("%d", &salesPerson);
	    printf("Enter product number (1 - 5):\n");
            scanf("%d" ,&product) ;
	    printf("Enter total sales of product:\n");
	    scanf("%lf", &value);
	    sales[ salesPerson ][ product ] += value;
  			     
      }
       	printf("The total sales for each sales person");
  	    printf("are displayed\nat the end of each row,");
  	    printf("and the total sales for each\nproduct ");
  	    printf("are displayed at the bottom of each column.\n");
  	    printf("%10d", 1);
        printf("%10d", 2);
	    printf("%10d", 3);
	    printf("%10d", 4);
	    printf("%10d", 5);
	    printf("%12d", "Total\n");
            for ( i = 1; i < PEOPLE; ++i ) {
                totalSales = 0.0;
		printf("%d", i);		
                for ( j = 1; j < PRODUCTS; ++j ) {
  		    totalSales += sales[ i ][ j ];   
  		    printf("%.2f", sales [i][j]);
  		    productSales[ j ] += sales[ i ][ j ];   
                 }
		     printf("%.2f", sales[i][j]);
  
            }
                 printf( "%.2f,","\nTotal",productSales );

                 for ( j = 2; j < PRODUCTS; ++j ) {
                     printf("%.2f", productSales[j]);
	         }
return 0;
  }

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Help with array code errors

Posted 15 February 2010 - 06:14 PM

Back to the book to resd how to actually WRITE a do-while loop!
Was This Post Helpful? 0
  • +
  • -

#9 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Help with array code errors

Posted 15 February 2010 - 09:02 PM

View PostJackOfAllTrades, on 15 February 2010 - 05:14 PM, said:

Back to the book to resd how to actually WRITE a do-while loop!

You are right Jack...thank you! I am just glad I realized what I did and fixed it before I saw your post.
Here is the code I have now...Please help tiiiiiiime is not on my side

My program compiles fine now however my output is like this:

Enter the sales person (1 - 4) Enter -1 to end input:1
Enter product number (1 - 5):
2
Enter total sales of product:
44
Enter the sales person (1 - 4) Enter -1 to end input:2
Enter product number (1 - 5):
2
Enter total sales of product:
33
Enter the sales person (1 - 4) Enter -1 to end input:3
Enter product number (1 - 5):
2
Enter total sales of product:
55
Enter the sales person (1 - 4) Enter -1 to end input:4
Enter product number (1 - 5):
2
Enter total sales of product:
66
The total sales for each sales person are displayed
at the end of each column,and the total sales for each product are displayed at
the bottom of each row.
1 2 3 4 1509563610.0000000.0000000.0000000.0
0000020.0000000.0000000.0000000.00000030.0000000.0000000.0000000.00000040.000000
0.0000000.000000-92559631349317831000000000000000000000000000000000000000000000.
0000000.000000,0.0000000.0000000.000000Press any key to continue . . .

#include <stdio.h>
     
      int main()
 {
      const int PEOPLE = 5, PRODUCTS = 6;
      double sales[ 5 ][ 6 ] = { 0.0 }, value;
      double totalSales, productSales[ 6 ] = { 0.0 };
      int salesPerson, product;
      int  i, j;
 
      salesPerson = 0;
      product =0;
      value = 0;
      
      do 
	  {
          printf("Enter the sales person (1 - 4) Enter -1 to end       input:");
          scanf("%d", &salesPerson);
	      printf("Enter product number (1 - 5):\n");
          scanf("%d" ,&product) ;
	      printf("Enter total sales of product:\n");
	      scanf("%lf", &value);
	      sales[ salesPerson ][ product ] += value;
  	  }
	  while (salesPerson = -1);

       	printf("The total sales for each sales person");
  	    printf("are displayed\nat the end of each row,");
  	    printf("and the total sales for each\nproduct ");
  	    printf("are displayed at the bottom of each column.\n");
  	    printf("%10d", 1);
        printf("%10d", 2);
	    printf("%10d", 3);
	    printf("%10d", 4);
	    printf("%10d", 5);
	    printf("%12d", "Total\n");
            for ( i = 1; i < PEOPLE; ++i ) {
                totalSales = 0.0;
		printf("%d", i);		
                for ( j = 1; j < PRODUCTS; ++j ) {
  		    totalSales += sales[ i ][ j ];   
  		    printf("%.2f", sales [i][j]);
  		    productSales[ j ] += sales[ i ][ j ];   
                 }
		     printf("%.2f", sales[i][j]);
  
            }
                 printf( "%.2f,","\nTotal",productSales );

                 for ( j = 2; j < PRODUCTS; ++j ) {
                     printf("%.2f", productSales[j]);
	         }
         
return 0;
  }

Was This Post Helpful? 0
  • +
  • -

#10 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Help with array code errors

Posted 15 February 2010 - 09:13 PM

View PostJackOfAllTrades, on 15 February 2010 - 05:14 PM, said:

Back to the book to resd how to actually WRITE a do-while loop!

You are right Jack...thank you! I am just glad I realized what I did and fixed it before I saw your post.
Here is the code I have now...Please help tiiiiiiime is not on my side

My program compiles fine now however my output is like this:

Enter the sales person (1 - 4) Enter -1 to end input:1
Enter product number (1 - 5):
2
Enter total sales of product:
44
Enter the sales person (1 - 4) Enter -1 to end input:2
Enter product number (1 - 5):
2
Enter total sales of product:
33
Enter the sales person (1 - 4) Enter -1 to end input:3
Enter product number (1 - 5):
2
Enter total sales of product:
55
Enter the sales person (1 - 4) Enter -1 to end input:4
Enter product number (1 - 5):
2
Enter total sales of product:
66
The total sales for each sales person are displayed
at the end of each column,and the total sales for each product are displayed at
the bottom of each row.
1 2 3 4 1509563610.0000000.0000000.0000000.0
0000020.0000000.0000000.0000000.00000030.0000000.0000000.0000000.00000040.000000
0.0000000.000000-92559631349317831000000000000000000000000000000000000000000000.
0000000.000000,0.0000000.0000000.000000Press any key to continue . . .

#include <stdio.h>
     
      int main()
 {
      const int PEOPLE = 5, PRODUCTS = 6;
      double sales[ 5 ][ 6 ] = { 0.0 }, value;
      double totalSales, productSales[ 6 ] = { 0.0 };
      int salesPerson, product;
      int  i, j;
 
      salesPerson = 0;
      product =0;
      value = 0;
      
      do 
	  {
          printf("Enter the sales person (1 - 4) Enter -1 to end       input:");
          scanf("%d", &salesPerson);
	      printf("Enter product number (1 - 5):\n");
          scanf("%d" ,&product) ;
	      printf("Enter total sales of product:\n");
	      scanf("%lf", &value);
	      sales[ salesPerson ][ product ] += value;
  	  }
	  while (salesPerson = -1);

       	printf("The total sales for each sales person");
  	    printf("are displayed\nat the end of each row,");
  	    printf("and the total sales for each\nproduct ");
  	    printf("are displayed at the bottom of each column.\n");
  	    printf("%10d", 1);
        printf("%10d", 2);
	    printf("%10d", 3);
	    printf("%10d", 4);
	    printf("%10d", 5);
	    printf("%12d", "Total\n");
            for ( i = 1; i < PEOPLE; ++i ) {
                totalSales = 0.0;
		printf("%d", i);		
                for ( j = 1; j < PRODUCTS; ++j ) {
  		    totalSales += sales[ i ][ j ];   
  		    printf("%.2f", sales [i][j]);
  		    productSales[ j ] += sales[ i ][ j ];   
                 }
		     printf("%.2f", sales[i][j]);
  
            }
                 printf( "%.2f,","\nTotal",productSales );

                 for ( j = 2; j < PRODUCTS; ++j ) {
                     printf("%.2f", productSales[j]);
	         }
         
return 0;
  }

Was This Post Helpful? 0
  • +
  • -

#11 newcuser  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-February 10

Re: Help with array code errors

Posted 15 February 2010 - 09:18 PM

posted wrong code a moment ago...sorry
#include <stdio.h>
     
      int main()
 {
      const int PEOPLE = 4, PRODUCTS = 5;
      double sales[5 ][ 4 ] = { 0.0 }, value;                         /*people in columns*/
      double totalSales, productSales[ 6 ] = { 0.0 };
      int salesPerson, product;
      int  i, j,counter;
 
      salesPerson = 0;
      product =0;
      value = 0;
      counter = 1;
	  totalSales =0;
      do 
	  {
          printf("Enter the sales person (1 - 4) Enter -1 to end       input:");
          scanf("%d", &salesPerson);
	      printf("Enter product number (1 - 5):\n");
          scanf("%d" ,&product) ;
	      printf("Enter total sales of product:\n");
	      scanf("%2f", &value);
	      sales[ product ][ salesPerson ] += value;             
  	  }
	  while (++counter <=4);

       	printf("The total sales for each sales person");
  	    printf("are displayed\nat the end of each column,");
  	    printf("and the total sales for each product ");
  	    printf("are displayed at the bottom of each row.\n");
  	    printf("%10d", 1);
        printf("%10d", 2);
	    printf("%10d", 3);
	    printf("%10d", 4);
	   	printf("%12d", "Total\n");
            for ( i = 1; i < PRODUCTS; ++i ) {
                
		printf("%d", i);		
                for ( j = 1; j < PEOPLE; ++j ) {
					totalSales+= sales[ i ][ j ];   
  		    printf("%2f", sales [i][j]);
  		    productSales[i] += sales[ i ][ j ];   
                 }
		     printf("%2f", sales[i][j]);
  
            }
                 printf( "%2f,","\nTotal",productSales );

                 for ( i = 2; i < PRODUCTS; ++i ) {
                     printf("%2f", productSales[i]);
	         }
         
return 0;
  }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1