Simple program to fill an array

In summary: When you give a summary, you need to include all of the information in the question. You did not do this, and as a result, you failed to provide a summary.
  • #1
cs23
66
0
#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 don't know how to initialize the array
 
Technology news on Phys.org
  • #2
cs23 said:
#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 don't know how to initialize the array


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?
 
  • #3
I've taken the liberty to put your code in [code ] tags and format it:
Code:
#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:
  • #4
jhae2.718 said:
theta[100] is an array of 0.0 one hundred times.
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:
double theta[100] = { 0 };
 
  • #5
cs23 said:
i don't know how to initialize the array

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)
 
  • #6
phiby said:
No - it's not. I don't see any code which puts 0.0 into the array.

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

cs23 said:
i don't know how to initialize the array

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:
[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:
  • #7
phiby said:
No - it's not. I don't see any code which puts 0.0 into the array.

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.
 
  • #8
phiby said:
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?

I'm trying to compute the sum of the array
 
  • #9
cs23 said:
I'm trying to compute the sum of the array

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

I know that double theta[100] = {0}; will fill each element with 0.
 
  • #11
cs23 said:
when i run it, i get -1.#QNA

I know that double theta[100] = {0}; will fill each element with 0.


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?
 
  • #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.
 
  • #13
cs23 said:
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.

Declare the array, and then use a for loop to put values 1, 2, ..., 100 into it.
 
  • #14
phiby said:
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?

cs23 said:
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.
Your answer was not what phiby asked. To be successful in programming, you have to pay attention to details.
 

What is a simple program to fill an array?

A simple program to fill an array refers to a set of instructions or code that is used to assign values to all the elements in an array. It is a basic programming concept that is used to store and organize data in a structured manner.

Why is it important to know how to fill an array?

Filling an array is an essential skill for any programmer as arrays are commonly used to store and manipulate data. Understanding how to fill an array allows for efficient data handling and can also improve the overall performance of a program.

What are the steps involved in filling an array?

The steps involved in filling an array include declaring the array, defining the size of the array, and using a loop to assign values to each element in the array. The loop is repeated until all the elements have been assigned a value.

Can you provide an example of a simple program to fill an array?

Yes, here is a simple program in Java to fill an array with integers:

int[] numbers = new int[5];

for(int i=0; i

numbers[i] = i+1;

}

This program declares an array of size 5 and uses a for loop to assign values to each element, starting from 1 to 5.

What are some common errors that can occur when filling an array?

Some common errors that can occur when filling an array include accessing an index that is out of bounds, not initializing the array properly, or not using a loop correctly to fill all the elements. These errors can result in a program crashing or producing unexpected results.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
1
Views
750
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
Back
Top