Thread: Having trouble with this program! Help please!

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    2

    Having trouble with this program! Help please!

    Hi!
    I'm very new to C and was given a problem to solve by my lecturer. Well, 3 problems actually. I got the first part to work, but I'm having a lot of trouble with the second part. Here's what the assignment is:
    "
    1. Write a program that creates an array of twenty doubles, then sets every even element of the array (beginning with 0) to the value '1.0' and every odd element to '2.0'. The program should then print all of the elements in the array, with each element on a new line.

    Such that:

    1st element: 1.0
    2nd element: 2.0
    3rd element: 1.0
    4th element: 2.0
    ...
    etc.


    2. Modify a copy of the program from Task 1, so that every third element is set to the value '4.0' and every fourth element set to the value '8.0'; and the contents of the array printed as before.

    Such that:

    1st element: 1.0
    2nd element: 2.0
    3rd element: 4.0
    4th element: 8.0
    5th element: 1.0
    6th element: 2.0


    ...
    etc.


    3. Modify a copy of the program from Task 2 such that the array elements are multiplied by their index and the results printed as before. This may be done in an additional loop after the previous tasks.

    Such that:

    1st element: 0.0 (index 0 multiplied by value 1.0)
    2nd element: 2.0 (index 1 multiplied by value 2.0)
    3rd element: 8.0 (index 2 multiplied by value 4.0)
    4th element: 24.0 (index 3 multiplied by value 8.0)
    5th element: 4.0 (index 4 multiplied by value 1.0)
    6th element: 10.0 (index 5 multiplied by value 2.0)


    ...
    etc.
    "
    Here's what I have so far:
    Code:
    #include <stdio.h>
     
    int main ()
    {
       double n[ 20 ]; /* n is an array of 20 doubles */
       int i,someInt; /*someInt will store values as it increments through the array*/
     
       /* Run someInt through array using this for loop */         
       for ( someInt = 0; someInt <= 20; someInt++ )
       {
            if(someInt==0) /* If the remainder of someInt divided by 2 is 0, then it must be even*/ 
          {
            n[someInt] = 1.0;
          }
          
            else if(someInt%4==0)
          {
            n[someInt] = 8.0;
          }
            else if(someInt%3==0)
          {
            n[someInt] = 4.0;
          }
             else if(someInt%2==0)
          {
            n[someInt] = 1.0;
          }
            else /* If the remainder of someInt divided by 2 is NOT 0, then it must be odd*/ 
          {
            n[someInt] = 2.0;
          }
       }
       
       // i will be used to loop through the array again to print each new element value in the array
        for (i = 0; i <= 20; i++)
        {
            printf("Element %d: %f\n", i, n[i]);
        }
       return 0;
    }
    I know that it involves using a modulo operator, but I can't get it to work so that the number order is exactly as the example in the question stated. Can anyone help?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you have 20 elements in your array, valid indexes are 0..19. 20 is out of bounds, and accessing an array out of bounds is undefined behavior (explanation). Your for loops should start at 0 and keep going while i < 20 (not <= 20).

    Don't use magic numbers. #define constants and use them instead.
    Code:
    #define ARRAY_SIZE 20
    ...
    double n[ARRAY_SIZE];
    
    for (i = 0; i < ARRAY_SIZE; i++)
        // do something with n[i]
    Now, you're right, it involves modulo. Let's take problem 2, which repeats with a period of 4. See if this program gives you a hint at what you should be checking with the modulo
    Code:
    $ cat foo.c
    #include <stdio.h>
    
    
    #define ARRAY_SIZE 20
    
    
    int main(void) {
        int i;
        for (i = 0; i < ARRAY_SIZE; i++) {
            printf("%2d %% 4 = %d\n", i, i % 4);
        }
        return 0;
    }
    $ gcc -g -Wall -std=c99 -o foo foo.c
    $ ./foo
     0 % 4 = 0
     1 % 4 = 1
     2 % 4 = 2
     3 % 4 = 3
     4 % 4 = 0
     5 % 4 = 1
     6 % 4 = 2
     7 % 4 = 3
     8 % 4 = 0
     9 % 4 = 1
    10 % 4 = 2
    11 % 4 = 3
    12 % 4 = 0
    13 % 4 = 1
    14 % 4 = 2
    15 % 4 = 3
    16 % 4 = 0
    17 % 4 = 1
    18 % 4 = 2
    19 % 4 = 3

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    2
    That's fantastic, thanks for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having a little trouble with my program
    By Kacikae Whitney in forum C Programming
    Replies: 4
    Last Post: 09-16-2012, 04:07 PM
  2. Having trouble with a C program
    By Dax-o-tron in forum C Programming
    Replies: 1
    Last Post: 08-16-2011, 11:20 PM
  3. My first program trouble
    By pal1ndr0me in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2006, 12:09 AM
  4. trouble with my first c program
    By mblazar in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 02:06 AM
  5. Having trouble with program
    By Bonedaddy in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2003, 06:14 PM

Tags for this Thread