Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple program to fill an array

  1. Jan 17, 2012 #1
    #include <stdio.h>
    int main()
    {
    double sum, theta[100];
    int i;
    sum = 0.0;
    i = 0;
    while (i < 100)
    {
    sum = sum + theta;
    i = i + 1;
    }
    printf("The sum of the array is %lf\n", sum);

    }

    when i run it i get a wrong answer....i dont know how to initialize the array
     
  2. jcsd
  3. Jan 17, 2012 #2


    What do you want to initialize it with?
    Your array is filled with random values and you are adding up those random values into the variable sum. It would help if you say what you are trying to do?
     
  4. Jan 17, 2012 #3

    jhae2.718

    User Avatar
    Gold Member

    I've taken the liberty to put your code in [code ] tags and format it:
    Code (Text):

    #include <stdio.h>

    int main()
    {
        double sum, theta[100];
        int i;
        sum = 0.0;
        i = 0;
        while (i < 100)
        {
            sum = sum + theta[i];
            i = i + 1;
        }
        printf("The sum of the array is %lf\n", sum);
    }
     
    theta[100] is an uninitialized array. You need to initialize it, as phiby said. You should also consider using a for loop instead of a while loop.
     
    Last edited: Jan 17, 2012
  5. Jan 17, 2012 #4

    No - it's not. I don't see any code which puts 0.0 into the array.

    If you want it to be array containing all 0's, this is the simplest way to do it

    Code (Text):


    double theta[100] = { 0 };

     
     
  6. Jan 17, 2012 #5

    I like Serena

    User Avatar
    Homework Helper

    Here are the 3 ways to initialize an array:

    double theta[3] = { 1.0, 2.0, 3.0 };

    theta[0] = 1.0;
    theta[1] = 2.0;
    theta[2] = 3.0;

    read from file (or standard input)
     
  7. Jan 17, 2012 #6

    jhae2.718

    User Avatar
    Gold Member

    You are of course correct. I was thinking of filling remaining elements of an array when not fully initialized.

    If there is a relationship between the elements you could also loop through. E.g. the following will produce an array of the first 10 squares:
    Code (Text):

    [COLOR="SeaGreen"][B]int[/B][/COLOR] array[[COLOR="Magenta"]10[/COLOR]];
    [COLOR="SeaGreen"][B]int[/B][/COLOR] i;
    [COLOR="DarkRed"][B]for[/B][/COLOR] (i = [COLOR="Magenta"]0[/COLOR]; i < [COLOR="Magenta"]10[/COLOR]; i++) {
            array[i] = (i + [COLOR="Magenta"]1[/COLOR]) * (i + [COLOR="Magenta"]1[/COLOR]);
    }
     
    I would recommend The C Programming Language (Amazon link b/c of Wiki blackout) by K&R as an introduction to C.
     
    Last edited: Jan 17, 2012
  8. Jan 18, 2012 #7

    Borek

    User Avatar

    Staff: Mentor

    While you are right there is no code initializing the array, values it contains don't have to be random. For example if you are using Microsoft C++ compiler (VisualStudio) in _DEBUG mode memory can be (or always is?) prefilled with things like 0xBAADF00D, 0xCDCDCDCD or 0xFDFDFDFD (plus some more) to make debugging easier.
     
  9. Jan 19, 2012 #8
    I'm trying to compute the sum of the array
     
  10. Jan 19, 2012 #9

    jhae2.718

    User Avatar
    Gold Member

    Do you just need an arbitrary array then?
     
  11. Jan 19, 2012 #10
    when i run it, i get -1.#QNA

    I know that double theta[100] = {0}; will fill each element with 0.
     
  12. Jan 19, 2012 #11

    Is it QNA or QNAN?
    QNAN is a quiet NAN. NAN is "Not a Number".

    Anyway, could you post the full program with which you are getting this error?

    Also why are you trying to find the sum of an array which is filled with 0s?
     
  13. Jan 20, 2012 #12
    Well this is the question that uses the code

    The previous program for computing the sum of an array may not produce a correct
    result since the elements of the array have not been properly initialized. Modify the program so that the array elements have values 1, 2, . . . , 100.
     
  14. Jan 21, 2012 #13

    Mark44

    Staff: Mentor

    Declare the array, and then use a for loop to put values 1, 2, ..., 100 into it.
     
  15. Jan 21, 2012 #14

    Mark44

    Staff: Mentor

    Your answer was not what phiby asked. To be successful in programming, you have to pay attention to details.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Simple program to fill an array
  1. Simple array problem (Replies: 10)

  2. An simple program (Replies: 3)

Loading...