This program is for receiving user int input and processing it with specified functions.
I am stuck on two functions.
Problem 1:
The minimum index value of user input. I can't get the input to return the correct index.
Problem 2:
Left array rotation. I have created the temp, but I can't get the input to list all values.
Program will run, just displays the wrong results.
#include <iostream>
using namespace std;
void getlist (int [], int &);
void putlist (const int [], int);
float meanAverage (const int [], int);
int minIndex (const int [], int);
void leftRotate (int [], int);
const int MAXSIZE = 50;
int i, length, sum, minI;
float average;
int mylist [MAXSIZE];
cout << "The minimum value index is " << minIndex << endl;
leftRotate (mylist, length);
system ("PAUSE");
return 0;
}
void getlist (int mylist[], int & lenght)
{
cout << "Enter the number of Array Elements, Maximum " << MAXSIZE << endl;
cin >> length;
for (i = 0; i < length; i ++)
{
cout << "Enter the next array element\n";
cin >> mylist [i];
}
}
void putlist (const int mylist [], int lenght)
{
cout << "Array elements\n";
for (i = 0; i < length; i ++)
cout << i << " " << mylist [i] << endl;
}
float meanAverage (const int mylist [], int length)
{
sum = 0;
for (i = 0; i < length; i ++)
sum = sum + mylist [i];
average = float (sum) / float (length) + 0.5;
return average;
}
int minIndex (const int mylist [MAXSIZE], int numels) //Problem 1
{
int i, min = mylist[0];
for (i = 1; i < mylist[0]; i++)
if (min > mylist[i])
min = mylist[i] ;
For the second problem, your program is doing exactly as it should be. All you are doing is siwtching some values and then only outputting 'i' and 'mylist[i]. You need to use a for loop to output all the values.
Secondly, you hava a for loop but i think you for got to put in the the curly braces in the 'leftRotate' function?
Oh and these two lines don't really make sense:
1 2
int temp, i = length;
for (i = 0; i < length; i ++)
Why give 'i' the value of length, and then give it the value of 0?
As for the second problem i don't really understand what you are trying to do. ARe you trying to find the lowest value in the array?
Enter the number of Array Elements, maximum 50
5
Enter the next array element
0
Enter the next array element
1
Enter the next array element
2
Enter the next array element
3
Enter the next array element
4
Array Elements
0 0 // correct
1 1 // correct
2 2 // correct
3 3 // correct
4 4 // correct
The avaerage is 2.5 // correct
The minimum value index is 5 // wrong
Left rotation of array // wrong, it gives no output.
look in the main of your program where you output the minimum index value. The reason you are getting wrong answers is because you are outputting the variable 'length', when you need to be outputting 'min'.
Also, lines 82/83 won't work because i has no value yet so you are likely to receive garbage values. You can never initialize variable with not-yet existent values e.g:
When you say the overloaded value to you mean a compiler wanrning or ? I've compiled it with no problem and from what you've explained it looks like tis working fine.
Oh and you don't need line 25 anymore, just line 27.