Thread: Array help

  1. #1
    oops it is socketboy
    Join Date
    Nov 2005
    Posts
    9

    Array help

    Hi,
    this is a school assignment and I am stuck.
    I have 3 one dimensional arrays, I think 2 dimensional might be easier, but this is what the assignment calls for.

    Now what I have is this:
    Code:
    main()
    {
    	int x = 0;
    	int y = 0;
    	int z = 0;
    	int empnum[30]={1, 4, 5, 7, 8, 12, 13, 14, 16, 22};
    	int days[30]={5, 8, 4, 5, 10, 5, 4, 10, 4, 4};
    	int vehicles[30]={30, 40, 20, 30, 70, 10, 15, 20, 40, 50};
    	
    		printf("\n Employee #");
    		printf(" Days");
    		printf(" Vehicles\n");
      for ( x = 0; x < 10;x++ )
    	{
    	  printf("%8d ", empnum[x]);
    		for ( y = 0; y < 10; y++ )
    			{
    				printf("%8d ", days[y]);
    					for ( z = 0; z < 10; z++ )
    					{
    							printf("%8d ", vehicles[z]);
    						
    			}
    		printf( "\n" );
    		} 
      }
    		
    
    	for (x=0; x<10; ++x)
    	{
    		printf("\n Enter Employee Number : ");
    		scanf("%d", &empnum[x]);
    	}
    	for (y=0; y<10; ++y)
    	{
    		printf("\n Enter Days : ");
    		scanf("%d", &empnum[y]);
    	}
    	for (z=0; z<10; ++z)
    	{
    		printf("\n Enter Number of Vehicles Tested : ");
    		scanf("%d", &empnum[z]);
    	}
    		return 0;
    }
    It is not final yet, in fact the part I am stuck on is in the very beginning.
    Code:
    printf("\n Employee #");
    		printf(" Days");
    		printf(" Vehicles\n");
      for ( x = 0; x < 10;x++ )
    	{
    	  printf("%8d ", empnum[x]);
    		for ( y = 0; y < 10; y++ )
    			{
    				printf("%8d ", days[y]);
    					for ( z = 0; z < 10; z++ )
    					{
    							printf("%8d ", vehicles[z]);
    						
    			}
    		printf( "\n" );
    		} 
      }
    When I had the printf statments just all by themselves it was lined up perfectly. Of course however all it returned was a row of 0's and not the data in the array. So I tried putting it together in a double nested for loop setup and the outcome is way messed up.
    I have messed around with lots of little tweaks, but I can't get a nice looking output.

    I have had it where the first two (employee # and days) output ok for one row, and then it outputs the entire vehicles array, then the next row is ok for the first two etc.

    Then i have had it where it is all diagonally slanted.

    Now I am not even sure what it is doing.
    Code:
    
     Employee # Days Vehicles
           1        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           7        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           8        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          12        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          13        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          14        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          16        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          22        5       30       40       20       30       70       10       15
           20       40       50
           8       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           5       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
          10       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
           4       30       40       20       30       70       10       15       20
           40       50
    So I am obviously a C noob because I can't figure out how to output a simple 1 dimensional array correctly.
    Can anyone give me some insight into what I might be doing wrong?
    Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Are you shootin' fer somethin' like this?
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int empnum[30]={1, 4, 5, 7, 8, 12, 13, 14, 16, 22};
       int days[30]={5, 8, 4, 5, 10, 5, 4, 10, 4, 4};
       int vehicles[30]={30, 40, 20, 30, 70, 10, 15, 20, 40, 50};
       int i;
       printf("%10s  %4s  %8s\n", "Employee #", "Days", "Vehicles");
       for ( i = 0; i < 10; ++i )
       {
          printf("%10d  %4d  %8d\n", empnum[i], days[i], vehicles[i]);
       }
       return 0;
    }
    
    /* my output
    Employee #  Days  Vehicles
             1     5        30
             4     8        40
             5     4        20
             7     5        30
             8    10        70
            12     5        10
            13     4        15
            14    10        20
            16     4        40
            22     4        50
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    oops it is socketboy
    Join Date
    Nov 2005
    Posts
    9
    Exactly what i am looking for. Thanks.

    I think I need to start looking for other resources for coding (like the main part of this site) because I was going off of my book and it did not have anything like that.
    Thank you much!

    The only way it was shown was through nested loops. That way is much simpler... perhaps when I learn more of the basics (I didn't know that you could have "i" be used for all three like that) I can figure things out better.
    Last edited by scoketboy; 12-09-2005 at 11:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM