I need help on two assignments that are due:

1. Initializing the elements of an array a1 and size n randomly, so that its elements have values between [1-45].

2. Initializing the elements of an array a2 and size n randomly, so that each element is between 0 and 50 and is a multiple of 5.

Yes. I just do not understand the logic that goes into the program.

Yes. I just do not understand the logic that goes into the program.

Posting the whole assignment will not help you or help us know what you have problem with. If you do not understand the logic, tell us which part that you don't understand. Tell us more about what might be the obstacle that prevent you from doing it.

I have just assumed n=10.

for the 1 st qs

 #include <iostream>
 using namespace std;
 int main()
 {
     int ar[10],a;
     for(a=0;a<10;a++)
     {
                                 ar[a]=(rand()%45)+1;
         cout<<ar[a]<<"\n";
     }
cin>>a;
return 0;
 }

for the 2 nd question

 #include <iostream>
 using namespace std;
 int main()
 {
     int ar[10],a;
     for(a=0;a<10;a++)
     {
         //ar[a]=(rand()%45)+1;
         ar[a]=(rand()%10);
         ar[a]=(ar[a]==0)?0:(ar[a]+1)*5;
         cout<<ar[a]<<"\n";
     }
cin>>a;
return 0;
 }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

See now what happens when you post without code tags?
No? Well, I see it: you get unwanted smileys in your code (and how is someone supposed to quickly read such code?).
So please wrap your code in tags :)

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Example 1:

#include <iostream>
using namespace std;
int main()
{
int ar[10],a;
for(a=0;a<10;a++)
{
ar[a]=(rand()%45)+1;
cout<<ar[a]<<"\n";
}
cin>>a;
return 0;
}

First of all, the assignment was to create an array, never said to display it. But besides that why did you do cin >> a; at the end of the loop ? If that is waiting for you to press enter why not just use cin.ignore(); But overall, i think you did this one right, what is the problem...? Why did you post. For us to check?

Example 1:
First of all, the assignment was to create an array, never said to display it. But besides that why did you do cin >> a; at the end of the loop ? If that is waiting for you to press enter why not just use cin.ignore(); But overall, i think you did this one right, what is the problem...? Why did you post. For us to check?

  1. Initializing the elements of an array a1 and size n randomly, so that its elements have values between [1-45].

I think the assignment want us to create an array with random size in runtime.

Size randomly? Then it would be completely different, you would probably use a pointer.

Sorry for the mis-information I posted another thread pertaining to the same problem.

This question has already been answered. Start a new discussion instead.