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.

Answered by mvmalderen 2,072 in a post from

Have you already tried to solve it?

Edit:: Where are you having problems with?

Answered by mvmalderen 2,072 in a post from

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

Answered by invisal 381 in a post from

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 …

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() …``````

## 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.

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?).

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, learning, and sharing knowledge.