Replace array with number if

Could someone help me with this problem I have? Basically I have to generate an array of 8 numbers and give all them a random value between 1-10. Then I have to generate an integer and give it a random value of 1-10. If the any number in that array is higher than the integer, it must be replaced with a value of 100. There must then be an output of the modified array. The array and output of the modified array must be done using a while loop.

I spent 3 hours over the course of 2 days to try and solve this problem and it is really frustrating considering it seem to be an easy question. Here is what my code looks like so far :

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
srand(time(0));
int a=rand()% 10+1;
int b[8];
int c[8];//scapegoat
int i=0;
cout<<a<<endl<<endl<<endl;
while(i<8)
{
b[i]=rand()% 10+1;
c[i]=b[i];
if(c[i]>a)
{
c[i]=100;
cout<<c[i]<<" ";
}

i++;
}
}

This code is the first I managed to churn out and it seem to be the best of all the methods that I tried, being able to convert the numbers to -1. However, it only shows how many numbers are -1 and not the whole array.

Help is very very much appreciated.
Last edited on
1
2
3
4
if(c[i]>a)
{  c[i]=100;
    cout<<c[i]<<" ";
}

How is c[i] ever going to be anything other than 100? You need to move the cout statement outside the if statement.
1
2
3
4
5
6
7
8
9
while(i<8)
{ b[i]=rand()% 10+1;
  c[i]=b[i];
  if (c[i]>a)
  { c[i]=100;
  }
  cout<<c[i]<<" ";
  i++;
}


BTW, please learn to use code tags (the <> button) when posting code.




Last edited on
Topic archived. No new replies allowed.