Thread: Storing and printing an array table

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    27

    Storing and printing an array table

    This is what I'm supposed to store as an array:
    236.41 382.01 529.51
    959.50 300.18 946.60
    362.72 250.97 950.26
    3.42 65.28 789.10
    165.89 735.90 187.78
    944.83 995.58 412.90
    894.48 183.67 787.93
    47.86 933.07 667.66
    851.40 138.38 680.80
    764.23 266.32 24.34
    So the code I currently have is:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    int i;
    double	arraysums [10] [3] = {  {236.41, 382.01, 529.51},
    							    {959.50, 300.18, 946.60},
    								{362.72, 250.97, 950.26},
    								{    3.42,   65.28, 789.10},
    								{165.89, 735.90, 187.78},
    								{944.83, 995.58, 412.90},
    								{894.48, 183.67, 787.93},
    								{  47.86, 933.07, 667.66},
    								{851.40, 138.38, 680.80},
    								{764.23, 266.32,  24.34}};
    
    							printf("%d", arraysums[10][3]);
    }
    This is not printing at all. I've specified which rows and columns to print, and I believe I've correctly stored the numbers in an array.
    After I get it printed i'm supposed to add all the numbers in each individual column and see which column I'm sure I can achieve this by using:
    Code:
    for (i=0; i<11; i++); 
    				sum = sum + arraysums [i][1];
    But I'm not sure. This problem is killing me. XD

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    Some of the mistakes you have done in your program,

    note one thing array will have the memory less than you allocate.For ex: arr[10] will have 0-9

    * printf("%f", arraysums[9][2]);
    you need to print the float or double value using %f.

    * for (i=0; i<11; i++);
    you put the unwanted semicolon here

    Try this,

    Code:
        printf("%f", arraysums[9][2]);
    
            for (i=0; i<10; i++)
                    sum = sum + arraysums[i][1];
                    printf("sum:%f",sum);

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    27
    Thanks. I see I made some pretty dumb mistakes. I've only recently started using 'double' and forgot it doesnt work with %d. I've made the changes to my code, and tried yours, but I get this weird error, but firstly here's my code so far:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    double sum = 0;
    double i = 0;
    double	arraysums [10] [3] = {  {236.41, 382.01, 529.51},
    							    {959.50, 300.18, 946.60},
    								{362.72, 250.97, 950.26},
    								{  3.42,  65.28, 789.10},
    								{165.89, 735.90, 187.78},
    								{944.83, 995.58, 412.90},
    								{894.48, 183.67, 787.93},
    								{ 47.86, 933.07, 667.66},
    								{851.40, 138.38, 680.80},
    								{764.23, 266.32,  24.34}};
    
    				 printf("%f", arraysums[9][2]);
    
           for (i=0; i<10; i++)
                   * sum = sum + arraysums[i][0];
                    printf("sum:%f",sum); 
    
    
    }
    I still cant get it to print the array, but from looking at my code, I cant see why.
    And I get an error on the * part, it says that the subscript is not of integral type. What does that mean? I have declared 'sum' at the top, so I'm not sure what the program is asking for.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    Actually you don't need to use the dereference operator (*) on the variable sum since it isn't a pointer, so you just want to use sum = sum + arraysums[row][col] or sum += arraysums[row][col], moreover if you want to show the sums of all columns what you just need to do is to use two nested loops like this:

    for i = 0 to total columns
    do sum = 0;
    for j = 0 to total rows
    do sum = sum + arraysums[j][i]
    print the sum of the current column

    plus you may want to put a return 0; at the end of your code, and use ints as array subscripts, not double, so i and j must be ints
    Bye

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  2. Storing and printing matrices
    By stevedawg85 in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 08:57 AM
  3. Help with Printing BMPs
    By simtron in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 09:21 AM
  4. Problem with storing and printing
    By kasun in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2004, 08:36 AM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM