Thread: C Programming using Arrays.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    10

    C Programming using Arrays.

    Having some issues with this problem.

    Given an array of 100 random numbers in the range 1 to 999, write a function for each of the following processes. In building the arrays, if the random number is evenly divided by 3 or 7, store it as a negative number.

    (a) Print the array ten values to a line. Make sure that the values are aligned in rows.
    (b) Print the odd values, ten to a line.
    (c) Print the values at the odd numbered index locations, ten to a line.
    (d) Return a count of the number of even values.
    (e) Return the sum of all values in the array.
    (f) Return the location of the smallest value in the array.
    (g) Copy all possible values to a new array. Then use process (a) above to print the new array.
    (h) Copy all negative values to a new array. Then use process (a) above to print the new array.

    This is what I have so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int ranary[100];
        int randnum;
        int randNos[100];
        
        for (int i = 0; i < 100; i++)
            {
                 do
                    {
                     randnum = rand() %100;
                    } while (randnum[ranary] == 1);
                 ranary[randnum] = 1;
                 randNos[i] = randnum;
                 
                 if(randNos[i] % 3 == 0 || randNos[i] % 7 == 0);
                 	{
               		 randNos[i] *= (-1);
                    }              
            }
            
    
        
        system("pause");
        return 0;
    }
    Not sure at all how to generate random number 1-999 correctly as It's hard to base it off the book: "Computer Science: A Structured Programming Approach Using C" when your teacher does things slightly different..

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Loading the array is very easy...
    Code:
    int rndarray[100];   // array
    int x;                   // index
    
    srand(time(NULL));  // initialize random sequencer
    
    for (x = 0; x < 100; x++)
       rndarray[x] = rand() % 1000;
    From there each of the manipulations will need to be in a separate subroutine... Don't try to do all this in one loop. Think in little blobs and get one thing done at a time...
    Last edited by CommonTater; 04-26-2011 at 12:43 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Mhm, Once I make the random array I'll be using functions for each part of the problem. The code that you posted, is that all I need for the random array? Is the if statement for the 3 and 7 correct?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yep, that's all it takes to initialize the array.

    For the if() clause ... The assignment says to store it as a negative number, not minus one...
    Code:
      for (x = 0; x < 100; x ++)
        if ( (rndarray[x] % 3 == 0) || (rndarray[x] % 7 == 0))   
          rndarray[x] = 0 - rndarray[x];
    And yes, I would do it in two separate loops although, strictly speaking, it's not necessary. It makes for easier debugging later.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    `time' cannot be used as a function

    I put #include <time.h> and identified it but I get that error.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Justin Ling View Post
    `time' cannot be used as a function

    I put #include <time.h> and identified it but I get that error.
    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main (void)
      { srand(time(NULL));
    
        return 0; }
    Does that give you the error?

    Pleas post your current code for this section...

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
        int rndarray[100];   // array
        int x;                   // index
        int time;
    
        srand(time(NULL));  // initialize random sequencer
    
        for (x = 0; x < 100; x++)
        {
            rndarray[x] = rand() % 1000;
        }
                
        for (x = 0; x < 100; x ++)
        {
            if ( (rndarray[x] % 3 == 0) || (rndarray[x] % 7 == 0))   
            rndarray[x] = 0 - rndarray[x];
        }   
    
        
        system("pause");
        return 0;
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Remove the int time declaration... it is overshadowing the time() function in time.h. The compiler only sees the newest declaration of things so when you made time into a variable, you prevented it from seeing time() as a function. Then it should work...

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Yup! I'll keep working on it, thanks for the help.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Justin Ling View Post
    Yup! I'll keep working on it, thanks for the help.
    Good stuff... get as far along as you can. If you run into trouble... post up your code and we'll see what we can do...

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Hmm not sure how to do c. what are the index locations? Gonna skip and do the rest.

    This is what I have so far.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void printarray(int rndarray[]);
    void oddarray(int rndarray[]);
    int countevenary(int rndarray[]);
    int sumary(int rndarray[]);
    
    
    int main()
    {
        int rndarray[100];   // array
        int x;                   // index
    
        srand(time(NULL));  // initialize random sequencer
    
        for (x = 0; x < 100; x++)
        {
            rndarray[x] = rand() % 1000;
        }
        
        printarray(rndarray);
        oddarray(rndarray);
    
        system("pause");
        return 0;
    }
    
    
    void printarray(int rndarray[])
    {
         int x;
         int numPrinted = 0;    
        for (x = 0; x < 100; x++)
        {
            if ((rndarray[x] % 3 == 0) || (rndarray[x] % 7 == 0))   
            rndarray[x] = 0 - rndarray[x];
        }
        
        printf("The array ten values to a line\n\n");
        
        for (x = 0; x < 100; x++)
        {
            printf("%3d ", rndarray[x]); // prints 10 numbers to a line
            if (numPrinted < 9)
                numPrinted++;
            else
            {
                printf("\n");
                numPrinted = 0;
            }
        }
    }
    
    void oddarray(int rndarray[])
    {
        int x;
        int numPrinted = 0;
        printf("\n\nThe odd values of the array\n\n");
        
        for (x = 0; x < 100; x++)
        {
            if (rndarray[x] % 2 == 0)
            printf("");
            else
            {
            printf("%3d ", rndarray[x]); // prints 10 numbers to a line
            if (numPrinted < 9)
                numPrinted++;
            else
            {
                printf("\n");
                numPrinted = 0;
            }
            }
    
        }
        
        printf("\n\n");
    }

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Justin Ling View Post
    Hmm not sure how to do c. what are the index locations? Gonna skip and do the rest.
    Code:
         type   name  indexes                    
           int    array  [100]
    Otherwise .... looking a lot better than at first...

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Definitely having issues now with H. and I.

    Code:
    void posary(int rndarray[])
    {
        int x;
        int y = 0;
        int numPrinted = 0;
        int posary[y];
        printf("\n\nThe positive numbers in the array\n\n");
        for (x = 0; x < 100; x++);
        {
            if (rndarray[x] > 0)
             {
                rndarray[x] = posary[y];
                printf("%3d ", posary[y]); // prints 10 numbers to a line
                y++;
            if (numPrinted < 9)
                numPrinted++;
            else
                {
                   printf("\n");
                   numPrinted = 0;
                }
             }
        }
    }
    Last edited by Justin Ling; 04-27-2011 at 03:29 PM.

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    bump.

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Announcements - C Programming
    Read rule #3. We'll get to your problem just as soon as we can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner in C programming (arrays)
    By greg677 in forum C Programming
    Replies: 12
    Last Post: 03-24-2010, 01:59 AM
  2. C programming problem - Arrays
    By Eman in forum C Programming
    Replies: 3
    Last Post: 11-22-2009, 06:02 PM
  3. C Programming Help - Using Arrays and Files
    By samdog45 in forum C Programming
    Replies: 1
    Last Post: 03-10-2008, 02:18 AM
  4. 2D arrays programming, please help
    By mfskratch in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 12:41 PM
  5. im a programming noob, having trouble with arrays
    By gb420 in forum C Programming
    Replies: 2
    Last Post: 04-12-2004, 12:38 PM