Random number array

This is a discussion on Random number array within the C++ Programming forums, part of the General Programming Boards category; Hey I have to build a random number array, heres the exact problem- void fillArray (int array[], int number) parameters ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    25

    Random number array

    Hey I have to build a random number array, heres the exact problem-

    void fillArray (int array[], int number)
    parameters : array of integers
    number of integers to store in the array
    description: generate "number" random numbers from 1 to 100 and store them in positions [0..number-1]
    So the array is always the same size, but may be filled with a different number of random values on each run

    void printArray (int array[], int number)
    parameters : array of integers
    number of integers stored in the array
    description: print the values in the array. The values appear 10 per line and are comma separated. The last value on each line does not have a comma after it. The last line may or may not have 10 values: for example, if the user asked for 15 numbers, then the last line would only have 5 values; or if the user asked for 92 values, then the output would be 9 lines containing 10 values and the last output line would have 2 values.
    I'm on the first part. My code is this-
    Code:
    #include <cstdlib> 
    #include <ctime> 
    #include <iostream>
    
    using namespace std;
    
    void main() 
    { 
    
     const int size;
     cout<< "how big do you want the array?" << endl;
     cin >> size;
    
    
    
     int array[size];
     
     srand((unsigned)time(0)); 
         
        for(int i=0; i<size; i++){ 
            array[i] = (rand()%100)+1; 
             
            cout << array[i] << endl;
     } 
    }
    I think I have to let the user choose the size of the array(indicated by the second part (void printArray)), and that is what is giving me problems. If I just define the constant or put an integer in for the array size it works fine, but this code does not compile and I would like to know why.
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Post the not working code as well as the compilation errors.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There are no variable length arrays in C++. The size of the array must be known at compile-time.

    What you need is to create the array dynamically:

    Code:
    int* array = new int[size];
    And don't forget to delete[] it before the program exits.

    Or else, make an array of some large size and don't allow the user pick a larger number.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is correct. The posted code MAY compile in some compilers, but doesn't conform to the standard.

    You need to either "supersize" the array (that is, make it 50000 entries, or something that is so much larger than anyone would ever need that it won't ever fill completely, and just use the portion of the array you actually need (e.g. 15 or 92 entries)).

    Or, use dynamic memory allocation with new and a pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I would not create the array dynamically. The assignment specifies a maximum of 100, so create a static array with 100 elements. Then use your size variable to tell where to stop when looping through it. (Just make sure that size is always less than or equal to 100.)

    If you do need a dynamic array, use a vector. The only time a dynamic array with new makes more sense than a vector is when the instructor won't allow the use of vector.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Thanks for the help. However I have a new problem. I thought this part was going to be easy but its hard.

    void printArray (int array[], int number)
    parameters : array of integers
    number of integers stored in the array
    description: print the values in the array. The values appear 10 per line and are comma separated. The last value on each line does not have a comma after it. The last line may or may not have 10 values: for example, if the user asked for 15 numbers, then the last line would only have 5 values; or if the user asked for 92 values, then the output would be 9 lines containing 10 values and the last output line would have 2 values.
    I've been trying all day to come up with a for loop to do it. I have a bunch of variations but here's the one I think is closest-

    Code:
    void printArray( int array[], int number)
    {
    
    
    for (int j=0; j<number; j= j +10){
      for (int i = 0; i<10; i++)
       cout<< array[i]<< " ";
    
    cout << endl;
    I just cant wrap my head around it.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. You only print the numbers from 1 to 10, a bunch of times. Maybe you should get j involved.
    2. I see a distressing lack of commas.
    3. You only print one new line, after the whole thing is finished, rather than after every ten numbers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Ok I have this-

    Code:
    void printArray( int array[], int number)
    {
    
    
    for (int j=0; j<number; j++){
      for (int i = 0; i<10; i++){
       cout<< array[j]<< ", ";
       j++;}
    cout << endl;
     }
    }
    It does the job but then I get extra numbers afterward that are out of my random number range.

    Arrgh this is giving me a headache

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You've got to think harder about your algorithm. What is the code doing now and what do you want it to do?

    You have the newline at the end of the first loop. Does that mean that each time the outer loop runs it should print out a single line? If so, how should you control that loop?

    The inner loop seems to print out a number and comma. So how do you control that loop to print out sequential values?

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    OK I got it, thanks for the help. However this assignment just keeps on getting harder and harder. I have 3 more functions to complete.

    Function1:
    I have to print the positions of the three consecutive values that have the largest average. If more than 3 consecutive values have the same average, print the positions with the lowest subscripts. I have to print the positions (subscripts), not the actual values.

    Here's my code so far-
    Code:
    void printConsecutive(int array[], int number)
    {
    
    int largeAvg;
    
    largeAvg = (array[0] + array[1] + array[2]);
    
    for (int i = 3; i < number; i = i + 3){
    if ( (array[i] + array[i++] + array[i = i + 2]) > largeAvg){
    
    int j = i++;
    int k = i + 2;
    
    cout<< i << ", " << j << ", " << k << endl;}
    
    }
    Its not giving me the results I want.

    Then I have to do this-

    Function 2:
    determine the percentage of the numbers from 1 to 100 that appear in the array. For example, if the array holds 100 integers and all the integers are unique (all numbers from 1 to 100 appear), the percentage would be 100%. The expected percentage is lower as duplicates may appear. To do this, each value from 1 to 100 is searched for in the array (using the search function) and if the number appears, a counter is incremented. Then the percentage is calculated. The result will be a value from 0.01 to 1.00. Note: if the user has asked for 10 values, then the maximum coverage in this case would be .1. This would mean that ten unique values between 1 and 100 are contained in the array. If, for example, the user asked for 50 values, then the max coverage would be 50% (.5), meaning than the array contained 50 values between 1 and 100 with no duplicates. If there were two duplicates, this would make the coverage only 48% (.48).

    Function 3:
    using the linear search function, search for the key in the array and return the position if found or -1 if not found
    This function will only be needed (and used) by the coverage function (above).

    Here's my code so far for function 3 ( I dont know how to implement it in function 2)-

    Code:
    int search (int list [], int size, int key)
    {
    int pos = 0;
    while (pos < size && list[pos] != key)
    pos++;
    if (pos == size)
    pos = -1;
    return pos;
    }

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    for (int i = 3; i < number; i = i + 3){
    if ( (array[i] + array[i++] + array[i = i + 2]) > largeAvg){
    
    int j = i++;
    int k = i + 2;
    
    cout<< i << ", " << j << ", " << k << endl;}
    Think about what happens to i here - I'm sure you don't really want to update it that much. Also, I expect you don't just want to look at index 0, (1, 2), 3 (4, 5), 6 (7, 8) where the number without parenthesis is the first one and the parenthesis ones are the ones you check if they are in sequence. Surely you want to check EVERY element if the next two are in sequence?

    I personally would use a for-loop rather than a while-loop in your search function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by matt_570
    array[i = i + 2]
    First of all, has this thread become a Matt convention? Secondly, his code is starting to resemble my style. Though I typically don't do those kinds of things accidentally.

    Code:
    void printConsecutive(int array[], int number)
    {
      int largeAvg = (array[0] + array[1] + array[2]);
    
      for (int i = 3; i < number; i += 3){
        if ((array[i] + array[i+1] + array[i + 2]) > largeAvg)
        {
          int j = i++;
          int k = i + 2;
    
          cout<< i << ", " << j << ", " << k << endl;}
        }
    
    // I assume the other brackets are correctly closed elsewhere.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    int search (int list [], int size, int key)
    {
      for(int pos = 0; pos < size; pos++)
        if(list[pos] == key)
          return pos;
      return -1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help regarding random number
    By Bargi in forum C Programming
    Replies: 6
    Last Post: 03-11-2009, 01:16 PM
  2. convert 32 bit number to array of digits
    By droseman in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 08:37 AM
  3. Create random array
    By Hunter_wow in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2007, 05:29 AM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. random number problems
    By lespaul5895 in forum C Programming
    Replies: 3
    Last Post: 07-05-2007, 02:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21