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.

Recommended Answers

All 13 Replies

Have you already tried to solve it?

Edit:: Where are you having problems with?

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.

Could you please post your code then?

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;
 }

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 :)

I am now and it was my first reply. Thanks for indicating. will take care in future..

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, all suggestions previously made by others

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.