void assignRandom(double *x, int n);

I have an assignment I have been working on and am not too sure where to go from here: (my problems are at the bottom of post)

"Write a function that assigns a random value to the first n elements of the array x using "void assignRandom(double *x, intn);". You must enter n from the keyboard adn display the one-dimensional array."

This is what I have so far and I have no idea which way to go. Or even how close I am.



#include <iostream>
#include <iomanip>

using namespace std;

void assignRandom(double *x, int n);  // the function declaration (prototype) 

int main ()

{
    int n;
    cout << "please enter size of array" << endl;
    cin >> n;

    cout << assignRandom << endl; 
    
    system ("pause");
    return 0;
   
}

/////////////////////////~~~~Assign Random Array~~~~//////////////////////////
                                                                            //  
void assignRandom(double *x, int n)                                         //  
{                                                                           //
                                                                            //
    int array [n];                                                          //
    srand(time(NULL));  // tells array to choose random number              //
                                                                            //
    for (int x=0; x<8; x++)                                                 //
    {                                                                       //
      array[x]=rand()%100 + 1;  //inserts rand # into array                 //
      cout << setw(4) << array [x];                                         //
        }                                                                   //
        cout<< endl << endl; // this is important there because             //
                          // it prints out after each line telling          //
                          // the loop to start on a new line.               //
}                                                                           //
                                                                            //
//////////////////////////////////////////////////////////////////////////////
 



This program actually runs but when I enter in the number for the array size, no matter what number I put in, it prints out the number 1 and that is all. Any advice would be greatly appreciated.
std::cout << assignRandom << endl;

There's your problem. The function doesn't return anything, AND you forgot the parameters. You can't possibly output the return if the function didn't return anything.

assignRandom(&some_variable,n); // Don't forget those parameters!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <iomanip>

using namespace std;

void assignRandom( int n);  // the function declaration (prototype) 

int main ()

{
    int n  = 0;

    cout << "please enter size of array" << endl;
    cin >> n;

    assignRandom(); 
    
    system ("pause");
    return 0;
   
}

/////////////////////////~~~~Assign Random Array~~~~//////////////////////////
                                                                            //  
void assignRandom( int n)                                         //  
{                                                                           //
                                                                            //
    int array [n];                                                          //
    srand(time(NULL));  // tells array to choose random number              //
                                                                            //
    for (int x=0; x<n; x++)                                                 //
    {                                                                       //
      array[x]=rand()%100 + 1;  //inserts rand # into array                 //
      cout << setw(4) << array [x];                                         //
        }                                                                   //
        cout<< endl << endl; // this is important there because             //
                          // it prints out after each line telling          //
                          // the loop to start on a new line.               //
}                      
Last edited on
Thanks for the help. This is what the final attempt looked like and it worked well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <iostream>
#include <iomanip>

using namespace std;

void assignRandom(double *x, int n);  // the function declaration (prototype) 

int main ()

{
    int n;
    double *ptr;
    cout << "please enter size of array" << endl;
    cin >> n;
    double array[n];
    ptr = &array[0];
    assignRandom(ptr,n);
    
    system ("pause");
    return 0;
   
}

/////////////////////////~~~~Assign Random Array~~~~//////////////////////////
                                                                            //  
void assignRandom(double *x, int n)                                         //  
{                                                                           //
    srand(time(NULL));  // tells array to choose random number              //
                                                                            //
    for (int i=0; i<n; i++)                                                 //
    {                                                                       //
      *(x+i) = rand()%100 + 1;  //inserts rand # into array                 //
      cout << setw(4) << *(x+i);                                            //
        }                                                                   //
        cout<< endl << endl;                                                //
}                                                                           //
                                                                            //
//////////////////////////////////////////////////////////////////////////////
Topic archived. No new replies allowed.