# Thread: I need help with array please.

1. ## I need help with array please.

I'm new here so im not sure how this works and i apologise if i wrote anything wrong but the question is:
Read 25 integers entered via keyboard and store them in a file "num.dat". Next, read the file and store the values in an array;
double all the values in the odd positions and add 3 to the even positions in the array. Write the new values to another file, "endin.dat".

This is what i have an its obviously not working, can someone help me please.

Code:
```#include<stdio.h>
int main (void){
int num[25];
int n;
int sum1;
int sum2;
n=num[25];
FILE * original = fopen("num.dat","w");
printf("Enter 25 numbers:\n", num[25]);

if (n%2==1){
sum1=n*2;
printf("Doubled odd numbers are %d\n",num[25]);
}
else {
sum2=n+3;
printf("Addition of three to even numbers %d\n", num[25]);
}
FILE * new = fopen("endin.dat","r");

fclose(original);
fclose(new);
return 0;
}```

2. It appears that you're trying to code everything at once. First, before you even start writing code, you should plan the logic on paper. Also, break down the problem into separate steps and solve them one at a time.

You are given a clear set of steps, which will help with this process.

Steps:

• Read 25 integers entered via keyboard and store them in a file "num.dat".
• Next, read the file and store the values in an array
• double all the values in the odd positions and add 3 to the even positions in the array.
• Write the new values to another file, "endin.dat".

The idea is to build your program up gradually, testing as you go (see A Development Process).

So I'd suggest the first thing you do is read the integers from stdin and store them in a file. Then read the numbers back from the file and print them to the screen. Compile, test, and fix this until you're certain that the numbers are being correctly stored and read from the file.

Then onto the next step. Update the program so that when you read the numbers from the file, they are stored into an array. Print the contents of the array. Compile, test, and fix this until you're certain that the numbers are being stored correctly into the array.

Continue this process, building up your program incrementally, compiling and testing along the way.

3. Would also suggest going back to your class notes and textbook for a refresh on arrays, because you're not currently close.

Popular pages Recent additions