# Help with Pointers and Arrays

Discussion in 'C++' started by silverdiamond, Apr 3, 2007.

1. ### silverdiamondNew Member

Joined:
Apr 3, 2007
Messages:
2
0
Trophy Points:
0
I'm fairly new to C++. I'm working on a few assignments but I'm having trouble with each of them. Just so everything gets explained thoroughly I'll post the assignments along with what I've coded and what I'm having problems with. The first assignment is this:

Write a C++ program to input ten integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop and the maximum should be determined as array element values are being input.

I ran the program but received a bunch of errors. I'm not sure what I'm doing wrong. I'm having the most problems with this program. This is what I coded so far:

Code:
```#include <iostream>
using namespace std;

int findMax(int [], int);

int main()
{
const int NUMPTS = 10;
int i, nums[NUMPTS], total = 0;
cout << "Enter a number: ";
cin >> nums;

{
cout << "\nThe maximum value is "
<< findMax(nums,NUMPTS) << endl;

return 0;
}
int findMax(int vals[], int numels);
{
int i, max = vals;

for (i = 1; i < numels; i++)
if (max < vals[i]) max = vals[i];

return max;
}
```
The next assignment runs but I'm not receiving the answer I'm supposed to. The assignment is:

Modify the findMax() function in Program 8.4 to locate the minimum value of the passed array.

I modified the program and the output is supposed to be 1 but for some reason I'm getting 2. Here is what I coded:

Code:
```#include <iostream>
using namespace std;

const int MINNUM = 5;

int findMin(int [MINNUM]);

int main()
{
int nums[MINNUM] = {2, 18, 1, 27, 16};

cout << "The minimum value is " << findMin(nums) << endl;

return 0;
}

int findMin(int vals[MINNUM])
{
int i, min = vals;

for (i = 1; i >= MINNUM; i++)
if (min >= vals[i]) min = vals[i];

return min;
}
```
Here is the last assignment:

Write a declaration to store the string “This is a sample” into an array named samtest. Include the declaration in a program that displays the values in samtest using a for loop that uses a pointer access to each element in the array.

I'm not sure if I coded this correctly or not. My end results displays the message with one letter per line. Here is my code:

Code:
```#include <iostream>
using namespace std;

int main()
{

const char samtest[] = {"This is a sample"};
int i;

cout << "The elements in the array are: " << endl;
for(i = 0; i <= 16; ++i)
cout << *(samtest + i) << endl;

return 0;
}
```
If anyone could explain to me what I'm doing wrong for any of these it would help me a lot. Thanks in advance.

2. ### DaWeiNew Member

Joined:
Dec 6, 2006
Messages:
835
5
Trophy Points:
0
Occupation:
Semi-retired EE
Location:
Texan now in Central NY
http://www.daweidesigns.com

Code:
`cin >> nums;`
nums is an array; you're asking for a single number. You can't put it in an entire array.

Code:
```int findMax(int vals[], int numels); [COLOR=Red]<---- deepsix the semicolon[/COLOR]
{ ....```
You also have a rogue '{' floating there. No dam' good.

In your second exaple, the for loop never runs. You are setting i to 1 and asking it to run only when i >= MINNUM. Anottagonnahappen less'n MINNUM is 1 or less.

Your last example displays one thing per line because you are telling it to.
Code:
`cout << *(samtest + i) << endl;"  [COLOR=Red]<-- endl is a combination newline and buffer flush.[/COLOR]`
Put the endl outside the loop if you want it there.

3. ### silverdiamondNew Member

Joined:
Apr 3, 2007
Messages:
2
0
Trophy Points:
0
The errors from the first program:

Code:
```E-8-1-8a.cpp:21: error: `vals' undeclared (first use this function)
E-8-1-8a.cpp:21: error: (Each undeclared identifier is reported only once for
each function it appears in.)
E-8-1-8a.cpp:23: error: `numels' undeclared (first use this function)
E-8-1-8a.cpp:27:2: warning: no newline at end of file
E-8-1-8a.cpp:27: error: syntax error at end of input
```

Joined:
Dec 6, 2006
Messages:
835