I need help with my homework. The first part is to create two arrays that are random. Afterward I need to creat afunction that finds the Minimun and Maximum of the two arrays.

Here is my code:

``````#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int
int main(void)

{

unsigned seed = time (0);

srand(seed);

cout<<"First Array"<<endl;

{int a1[45] =  {1 + rand () % 45};

cout <<a1[45]<<endl;}

cout<<"SecondArray"<<endl;

{int a2[50] = {rand () %50};

if (a2[50] % 5 ==0)

cout<<a2[50]<<endl;}

cout<<"Third Array"<<endl;

int max(0);

if (a1[45] > max)

max = a1[45];{

cout<<"Maximum of a1[45] = " <<is]
``````

Edited by Reverend Jim: Fixed formatting

6
Contributors
10
Replies
11
Views
9 Years
Discussion Span
Last Post by csurfer
Featured Replies
• 4

Had a SNAFU while responding before. Luckily I had copied and pasted. Ditto what Walt P said. You need to describe the problems. Salem had a good thread that he linked and which I bookmarked, but subsequently lost. Bummer. If I had it, I'd link it, but I'll recreate the …

``````#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int
int main(void)

{

unsigned seed = time (0);

srand(seed);

cout<<"First Array"<<endl;

{int a1[45] =  {1 + rand () % 45};

cout <<a1[45]<<endl;}

cout<<"SecondArray"<<endl;

{int a2[50] = {rand () %50};

if (a2[50] % 5 ==0)

cout<<a2[50]<<endl;}

cout<<"Third Array"<<endl;

int max(0);

if (a1[45] > max)

max = a1[45];{

cout<<"Maximum of a1[45] = " <<is``````

I don't see any loops at all in your code. You need them. I see a lot of seemingly pointless brackets in the code, which may not hurt anything (on the other hand, they might), , but don't add anything to the code and confuse the reader.

``{int a1[45] = {1 + rand () % 45};``

Huh?

You can do this:

``````int a1[45];   // creates an array of 45 integers.
a1[44] = 1 + rand () % 45;  // assigns a value to ONE element.``````

I imagine you are trying to do this?

``````int a1[45];
for (int i = 0; i < 45; i++)
a1[i] = 1 + rand () % 45;``````

This fills in the array elements with random numbers from 1 to 45.

Hey, why not telling him to post using code tags?

To the OP:

1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled "Read Me: Read This Before Posting"
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in

Thanks for the tip on improving the array but how do you find the minimum and maximum of an array?

You are not lost after coding this much you are lost from the beginning...

Hell lot of mistakes :

1> `[#include <iostream>` Typing mistake

2>

``````int
int main(void)``````

What is this form ???

3> `cout <<a1[45]<<endl;` Segmentation fault illegal memory referenced alloted memory is from `a1[0] to a1[44]` for declaration `int a1[45];` 4>

``````if (a2[50] % 5 ==0)
cout<<a2[50]<<endl;``````

Segmentation fault again

5> `int max(0);` What is this exactly???

6>

``````if (a1[45] > max)
max = a1[45];``````

Segmentation fault again.

And USE CODE TAGS and for finding minimum and maximum you can follow this algorithm

``````int min,max;
//Loop through a1[] first and initialize min=max=a1[0]; Now for every i ahead
if(a1[i]<min) min=a1[i];
if(a1[i]>max) max=a1[i];

//Now loop through a2[] ,for every i
if(a2[i]<min) min=a2[i];
if(a2[i]>max) max=a2[i];

//YOu'll have both min and max values for both arrays combined.``````

You're jumping the gun. You need to repost the corrected code, code that compiles, before you worry about the max and the min. You don't move on to task C before making sure tasks A and B work. Do a little at a time and make sure you understand what's going on.

Here's what I have so far.

``````#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int findmax (int a1[45], int a2[50], int i, int j);
int findmin (int a1[45], int a2[50], int i, int j);

int main(void)

{

unsigned seed = time (0);

srand(seed);

cout<<"First Array"<<endl;

int a1[45];
for (int i = 0; i < 45; i++)
a1[i] = 1 + rand () % 45;

cout <<a1[i]<<endl;}

cout<<"Second Array"<<endl;

int a2[50];
for (int j = 0; j < 50; i++)
a2[j] = 1 + rand () % 50;

if (a2[j] % 5 ==0)

cout<<a2[j]<<endl;}

cout<<"Third Arrary"<<endl;

cout<<max1<<endl;

cout<<"Forth Arrary"<<endl;

cout<<max2<<endl;

cout<<"Fifth Array"<<endl;

cout<<min1<<endl;

int findmax(int a1[i], int i)
{
int max1;
min = 1;
for (i=0; i<45; i++)
{
if(a1[i] > min)
max1=a1[i];

}
return max1;
}

int findmax(int a2[j], int j)
{
int max2;
min = 1;
for (j=0; j<50; j++)
{
if(a2[j] > min)
max2=a1[j];

}
return max2;
}

int findmin(int a1[i], int i)
{
int i, min1;
min = 45;
for (i=0; i<45; i++)
{
if(a1[i] < min)
min1=a1[i];

}
return min1;
}
int findmin(int a2[j], int j)
{
int min2;
min = 50;
for (j=0; i<50; j++)
{
if(a2[j] < min)
min2=a2[j];

}
return min2;
}``````

Here's what I have so far.

And are you still having problems? Are we supposed to use our psychic powers or would you like to explain fully what the problems are now?

OP has another thread and a source code at post #7 belongs to this thread.

Had a SNAFU while responding before. Luckily I had copied and pasted.

Ditto what Walt P said. You need to describe the problems.

Salem had a good thread that he linked and which I bookmarked, but subsequently lost. Bummer. If I had it, I'd link it, but I'll recreate the overall idea.

Program in stages. Do a little but, compile, then fix errors.

First step:

``````int main ()
{
return 0;
}
``````

Next step, add some #includes and namespace statements.

``````#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{
return 0;
}
``````

Compile again. Works great. Add some function declarations.

``````#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int findmax (int a1[45], int a2[50], int i, int j);
int findmin (int a1[45], int a2[50], int i, int j);

int main ()
{
return 0;
}
``````

Compiles fine, good to go, now add a function:

``````#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int findmax (int a1[45], int a2[50], int i, int j);
int findmin (int a1[45], int a2[50], int i, int j);

int main ()
{
return 0;
}

int findmax(int a1[i], int i)
{
int max1;
min = 1;
for (i=0; i<45; i++)
{
if(a1[i] > min)
max1=a1[i];

}
return max1;
}
``````

Bingo! First problem:

int findmax(int a1[i], int i)

'i' was not declared in this scope.

Fix it. Recompile.

``````#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int findmax (int a1[45], int a2[50], int i, int j);
int findmin (int a1[45], int a2[50], int i, int j);

int main ()
{
return 0;
}

int findmax(int a1[], int i)
{
int max1;
min = 1;
for (i=0; i<45; i++)
{
if(a1[i] > min)
max1=a1[i];

}
return max1;
}

min = 1;
``````

invalid operands of types `int' and`<unknown type>' to binary `operator>'

Fix it, recompile, see if there are more errors, etc. The whole point is that you do a little at a time and you don't move onto the next step until the previous step works, which is why I suggested you not try to find the max and min till everything else works, because you also have a problem here:

``````int a1[45];
for (int i = 0; i < 45; i++)
a1[i] = 1 + rand () % 45;

cout <<a1[i]<<endl;}
``````

This problem will be far easier to fix if you indent your code in a way so that loops are indented consistently and brackets are on their own line (styles differ. I prefer to have each bracket have its own line):

``````int a1[45];
for (int i = 0; i < 45; i++)
a1[i] = 1 + rand () % 45;
cout <<a1[i]<<endl;
}
``````

You'll immediately notice the lack of a starting bracket. Add it.

``````int a1[45];
for (int i = 0; i < 45; i++)
{
a1[i] = 1 + rand () % 45;
cout <<a1[i]<<endl;
}
``````

One step at a time, compile often, constant formatting style, look for bracket problems, read and interpret your error messages carefully, solve one error at a time and recompile.

Edited by Reverend Jim: Fixed formatting