Question

In: Computer Science

How to inhabit the int array num with random numbers using a function that you then...

How to inhabit the int array num with random numbers using a function that you then invoke from the main method.

I already created a function that generates an array of 5 random integers.

/* int i;

  int arrayName[5];

    for(i= 0;i < 6; i++ ){

           //type arrayName[arraysize];

           arrayName[i] = rand()% 200;

      printf("array[%d] = %d\n",i,arrayName[i]);

*/

// use rand() % some_number to generate a random number
int num[5];
int main(int argc, char **argv)
{
// hint
// function invocation goes here

return 0;
}

Solutions

Expert Solution

Basically here you want to store the randomly generated numbers from the function into the int num array. So in order to do that, there should be a function return type and you should return the int array. In C, functions cant return arrays. But there are 3 ways in which we can return array. I am using static array to do that. You can also use dynamic allocated array or struct to do that. Let us see that in the following code.

#include <stdio.h>
#include<stdlib.h>
int* generate() //return type is pointer
{
     int i;
     static int arrayName[5]; //declare it as static
    for(i= 0;i < 6; i++ ){
           arrayName[i] = rand()% 200; //generate random integers and store it in the arrayName
          //printf("array[%d] = %d\n",i,arrayName[i]);
    }
    return arrayName; ///return arrayName
}
int* num;
int main(int argc, char **argv)
{
    int i;
    num=generate();//invoke the function here. Now the values of arrayName will be stored in int array pointer
    
    //optional. i used for loop here to check the array values
    for(i= 0;i < 6; i++ ){
      printf("array[%d] = %d\n",i,num[i]);//now print the values 
    }
    //optional
   

    return 0;
}

I am also attaching the output and code screenshot for your reference.

Output and code screenshot:

#Please don't forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.


Related Solutions

how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
Write a function that will generate an array of random numbers. It needs to:
DO IN C++Write a function that will generate an array of random numbers. It needs to:Take 3 integers as parameters-The first is the minimum value-the second is the maximum value-the third is the size of the new array-create a dynamically allocated array of the correct size-generate a random number (between min and max) for each element in the array-return a pointer to the arrayCreate a main() function that tests the above function and displays the values in the random array.
(True or False) The following function will compile. void print(int num) { return num+1; } Group...
(True or False) The following function will compile. void print(int num) { return num+1; } Group of answer choices True False Flag this Question Question 2 10 pts (True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; } Group of answer...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function which takes one parameter int num, and prints out a countdown timer with...
Write a function which takes one parameter int num, and prints out a countdown timer with minutes and seconds separated by a colon (:). It should print out one line for each second elapsed and then pause one second before printing out the next line. A few things to note: - You can assume that calling the function usleep(1000000) makes the program pause for one second - It should count down from num minutes:zero seconds to zero minutes:zero seconds -...
Using the following array: //may be declared outside of the main function const int NUM_Games =4;...
Using the following array: //may be declared outside of the main function const int NUM_Games =4; //may only be declared within the main function int scores[NUM_GAMES] = {122, 76, 92, 143}; Write a C++ program to run a menu-driven program with the following choices: 1) Display the scores 2) Change a score 3) Display game with the highest score 4) Display a sorted list of the scores 5) Quit Write a function called getValidScore that allows a user to enter...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and...
Write Java program Lab43.java which declares and initializes numeric array of 5 elements (int num[5]) and produces as output the sum of integers in an array, the largest and the smallest element in an array. Your program should contain the following methods: public static int sum(int[]) public static int findLargest(int[]) public static int findSmallest(int[])
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT