Thread: I need help with array please.

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    2

    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. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    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. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,671
    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 subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  2. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM