Function explanation: Write a function that fills the gData array with random nu
ID: 3687285 • Letter: F
Question
Function explanation: Write a function that fills the gData array with random numbers between 1 and MAX_RANDOM_NUMBER
QUESTION: If indexForZero is valid and non-negative, set the value at that index to zero.
So basically I'm asking how to set the value at the index to zero? Which is a conditional statement I believe. The function is below
void GenerateInput(int size, int indexForZero) {
for(i=0;i<size;i++)
{
gData[i] = GetRand(1, MAX_RANDOM_NUMBER); // GetRand just returns a random int from 1 to 1,000,000)
}
}
Explanation / Answer
You need to use IF conditional statement for checking the number whether negative or non- negative. I have used if statement to put the condition in your code.
void GenerateInput(int size, int indexForZero) {
for(i=0;i<size;i++)
{
if(indexForZero >= 0){ //If it is non negative number then only will allow to set for value at index.
gData[i] = GetRand(1, MAX_RANDOM_NUMBER); // GetRand just returns a random int from 1 to 1,000,000)
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.