Help with understanding assignment

I am not asking for someone to write the code for me for this shell outline of a project assignment, I just don't understand what it is supposed to do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//  CS 200   Lab 10   pointer.cpp
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace::std;

int largeArray(const int [], int);
int largePointer(const int * , int);


void fillArray(int * , int howMany);

void printArray(const char *,ostream &, const int *, int howMany);



//  VALUES FOR LOW AND HIGH OF RANDOM DATA
const int low = 50;
const int high = 90;

void main()
{
	const int MAX_SIZE = 40;

    int info[MAX_SIZE];

	int used;
	cout << "How many elements in the array? ";

	cin >> used;
	while ( used <= 0 || used > MAX_SIZE )
	{
		cout << "Enter a value between 1 and " << MAX_SIZE << endl;
		cout << "How many elements in the array? ";
    	cin >> used;
	}

	fillArray(info,used);
	printArray("Array of data",cout,info,used);

    cout << "The largest element using subscripts is " << largeArray(info,used) << endl;

	cout << "The largest element using pointers is " << largePointer(info,used) << endl;

}   // end main ***************************



void printArray(const char * m,ostream & Out,const int * p, int hm)
{
    Out << m << endl;
	for(int i = 0; i < hm; i++)
	{
		Out << p[i] << endl;
	}
}


void fillArray(int  *  p , int howMany)  // as parmeters pointers and arrays are the same
{
 	 int range = high - low + 1;
	 srand(time(0));
	 for( int i = 0; i < howMany; i++)
	 {
		 p[i] =  rand() % range + low;
	 }
}


int largeArray(const int data[], int howMany)   // use subscripts
{
	int largest = 0;

	//   ******   STUDENT WILL WRITE THE CODE TO FIND THE LARGEST NUMBER
	//   ******        USE SUBSCRIPTS TO ACCESS THE ELEMENTS
	
	return largest;
}
int largePointer(const int  * data, int howMany)   // use pointers
{
	int largest = 0;

	//   ******   STUDENT WILL WRITE THE CODE TO FIND THE LARGEST NUMBER
	//   ******        USE POINTERS TO ACCESS THE ELEMENTS
	
	return largest;

}
closed account (j3Rz8vqX)
The program will randomly fill an array, of size (users choice) with random values between the High(90) and Low(50).

Your professor has outlined two functions:

1) A function to find the largest number - using subscripts, aka data[].
-int largest = 0; will contain the largest integer value found; hence why there is a return statement with largest being returned.

2) A function to find the largest number - using pointers, aka *data.
-int largest = 0; will contain the largest integer value found; hence why there is a return statement with largest being returned.

The task isn't too difficult, it is testing your understanding of the behaviors of pointers, and possibly your currently knowledge of arrays.

---

Some tips - in case you're reading too deeply into the behavior of the problem:

"info" is declared on line 26 as an array of integers with a max size of 40.

"used" is declared on line 28, and modified by the user on line 31; it will determine how much of the array is to be used - less than 40.

The while loop on line 32-37 is a simple data integrity test - ensuring the value entered for "used" is acceptable.
Last edited on
Out << m << endl;

What is out?
Id hazard a guess and say its meant to be cout
closed account (j3Rz8vqX)
1
2
3
4
5
//Line: 40
printArray("Array of data",cout,info,used);

//Line: 50
void printArray(const char * m,ostream & Out,const int * p, int hm)


"cout" was passed to the function as "Out".
"Out" is simply "cout"

Consider it an alias for the printArray scope.
I figured it out, I had to re-write the whole program bit by bit so I could understand what was going on. I get it now. I feel a bit slow for having to do that, should I "get it" right away?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <ctime>
#include <cstdlib>
#include <iostream>

using namespace std;

const int low = 50;
const int high = 90;

const int MAX_SIZE = 40;

void fillArray(int * p, int howMany);
void printArray(const char * m, ostream & out, const int * p, int howMany);
int largeArray(const int data[], int howMany);
int largePointer ( const int * data, int howMany);

int main()
{
    int info[MAX_SIZE];
    
    int used;
    cout << "How many elements in the array?" << endl;
    cin >> used;
    while ( used <= 0 || used > MAX_SIZE )
    {
          cout << "You must enter a number between 1 and " << MAX_SIZE << endl;
          cout << "How many elements in the array?" << endl;
          cin >> used;
    }
    fillArray(info, used);
    printArray("Array of data", cout, info, used);
    cout << "The largest element using subscripts is " << largeArray(info,used) << endl;
    cout << "The largest element using pointers is " << largePointer(info,used) << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

void fillArray(int * p, int howMany)
{
     int range = high - low + 1;
     srand(time(0));
     for ( int x = 0; x < howMany; x++ )
     {
         p[x] = rand() % range + low;
     }
}

void printArray(const char * m, ostream & out, const int * p, int howMany)
{
     out << m << endl;
     for ( int x = 0; x < howMany; x++ )
     {
         out << p[x] << endl;
     } 
}

int largeArray(const int data[], int howMany)
{
    int largest = 0;
    
    for ( int x = 0; x < howMany; x++ )
    {
        if ( data[x] > largest )
        {
             largest = data[x];
        }
    }
    return largest;
}

int largePointer ( const int * data, int howMany)
{
    int largest = 0;
    
    for ( int x = 0; x < howMany; x++ )
    {
        if ( data[x] > largest )
        {
             largest = data[x];
        }
    }
    return largest;
}
closed account (j3Rz8vqX)
Everyone interprets differently.

Deadlines, stress, and experiences play a thing or two in interpretation.

I did not double checked your code, but I'm sure you've got it.

Good Job.

Some Tips:
-Read comments from prior programmer(s) or your professor; they should inform you of your task or make your task possibly less difficult.

-Understand the problem - in this case it was provided in the comments. Knowing the problem is the same as knowing the solution. Simply put you can ensure you know the problem when you've got the solution (solutions can be improvised - follow your blue print).

-Never jump straight into the code, behavior may become too difficult to follow; especially when including recursion, multiprocessing, abstracts, scope narrowing/spanding, and forced exits.
Topic archived. No new replies allowed.