Thread: Two dimensional array not changing values

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    26

    Two dimensional array not changing values

    The book that I'm reading has end of chapter exercises. And for this one it wants me to

    "Define a two-dimensional array, data[12][5], of type double. Initialize the elements in the first column with values from 2.0 to 3.0 inclusive in steps of 0.1. If the first element in a row has
    the value x, populate the remaining elements in each row with the values 1/x, x^2 , x^3 , and x^4 . Output
    the values in the array with each row on a separate line and with a heading for each column."

    So I wrote this, which isn't working as the output either equal to 1 or 0. Any ideas what I did wrong?

    Code:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    {
        double data[12][5]={0}; /*initialize array*/
    
    
        for(int i = 0; i < 12; i++) /*loop 12 times*/
        {
            data[i][0]= 2.0+(0.1*i); /*first element should go up by 0.1 for each dimension(??)*/
            for(int i2 = 0; i2 < 5; i2++)
            {
                if(i2 == 0)
                {
                    data[i][i2]+= 0; /*leave unchanged*/
                }
                if(i2 == 1)
                {
                    data[i][i2]= 1/data[i][0];  /*divide 1 by the value held in data[i][0]*/
                }
                else
                {
                    data[i][i2]= pow(data[i][0], i2); /*set it the the appropriate power*/
                }
            }
        }
        printf("data-1|data-2|data-3|data-4|data-5\n"); /*output*/
        for(int i = 0; i < 12; i++)
        {
            printf("%.4lf|%.4lf|%.4lf|%.4lf|%.4lf\n", data[i][0], data[i][1], data[i][2], data[i][3],data[i][4]);
        }
        
        return 0;
    }
    Output:
    Code:
    data-1|data-2|data-3|data-4|data-5
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    1.0000|1.0000|1.0000|1.0000|1.0000
    Last edited by YayIguess; 08-31-2015 at 12:45 PM.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You should try to not do so much at once. Write code that initializes the first column first and once you get that working, proceed with the rest of the program.

    Edit : Consider something more like this :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main( void )
    {
        double x[ 12 ][ 5 ] = { { 0 } };
    
    
        for ( size_t i = 0; i < 12; ++i )
        {
            x[ i ][ 0 ] = 2 + .1 * i;
        }
    
    
        for ( size_t i = 0; i < 12; ++i )
        {
            printf( "%.4lf\n", x[ i ][ 0 ] );
        }
    
    
        return 0;
    }
    This code was compiled with :
    Code:
    gcc -std=c11 -Wall -Wextra -pedantic -O3 -o array array.c
    and the output is :
    Code:
    2.0000
    2.1000
    2.2000
    2.3000
    2.4000
    2.5000
    2.6000
    2.7000
    2.8000
    2.9000
    3.0000
    3.1000
    Last edited by MutantJohn; 08-31-2015 at 01:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a 2 dimensional array, my 0,0 spot keeps changing
    By brownie5 in forum C Programming
    Replies: 7
    Last Post: 01-12-2015, 06:44 PM
  2. Scanning a 2 dimensional array for duplicate values
    By McZiploc in forum C Programming
    Replies: 1
    Last Post: 03-27-2013, 12:26 AM
  3. changing the values of the array elements
    By yugidallin in forum C++ Programming
    Replies: 4
    Last Post: 01-01-2013, 07:27 AM
  4. Help changing the values inside an array.
    By zbred in forum C Programming
    Replies: 3
    Last Post: 04-10-2011, 11:06 PM
  5. changing array values using pointers
    By dford425 in forum C Programming
    Replies: 8
    Last Post: 01-15-2011, 10:45 PM

Tags for this Thread