Arrays and Functions

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];

int main ( void )
{

getlist (mylist, length);
putlist (mylist, length);
meanAverage (mylist, length);

cout << "The average is " << average << endl;

minIndex (mylist, minI);

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] ;

return min;
}
void leftRotate (int mylist [], int length) //Problem 2
{

int temp, i = length;
for (i = 0; i < length; i ++)

temp = mylist[0];
mylist[0] = mylist[i];
mylist[i] = temp;

cout << "Left rotation of array\n";
cout << "Array rotation:\n" << i << " " << mylist[i] <<endl;

}
Last edited on
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?
Last edited on
For the second problem, I need the actually cin index of the lowest value.
Sorry, i was meant to write ... As for the FIRST problem, I don't understand it. Please explain.

Change your leftRotate function to this and then run your program:
1
2
3
4
5
6
{
cout << "Left rotation of array\n";
cout << "Array rotation: ";
for (i=length-1; i>=0; i--)
  cout << i << ' ';
}


Is this what you wanted?
Ok for the first problem.
ex: user input is 13, 24, 9
it will be printed as follows:
0 13
1 24
2 9

I need the lowest input's index (element) value returned.
in the example, that would return index 2
because 9 is the lowest input value.
Sorry, i was meant to write ... As for the FIRST problem, I don't understand it. Please explain.

Change your leftRotate function to this and then run your program:

Is this what you wanted?
|


Close. What i need to do is rotate the list not the index.
closed account (z05DSL3A)
You need to look at how you are calling your functions...

1
2
3
4
5
6
7
8
9
10
11
12
    int bar()
    {
        return 5;
    }

    //...

    int foo;
    foo = bar();
    
    cout << foo;
Oh i see what you mean.

1
2
3
4
5
6
7
8
9
int i, min = mylist[0];

for(i=0;i<length(mylist);i++)  
  {
     if(min>mylist[i])
        min = mylist[i];
  }

return min;


Try that ^^
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
90
91
#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;
float average;
int mylist [MAXSIZE];

int main ( void )
{
    
    getlist (mylist, length);
    putlist (mylist, length);
    meanAverage (mylist, length);
    
                cout << "The average is " << average << endl;
    
    minIndex (mylist, length);
                
                cout << "The minimum value index is " << length  << endl;
                
    leftRotate (mylist, length);
               
               for(int i=0; i < mylist [i]; ++i)
		       cout << i << " " << mylist[i] << endl;   
               
      
    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 length) //Problem 1
{       
    int i, min = mylist[0];
    
    for (i = 0; i < mylist[0]; i++)
     
        if (min > mylist[i])
        min = mylist[i];
     
    return min;
}
void leftRotate (int x[], int length)
{
	int temp;
	if (length == 0);
	else
    {
		temp = x[0];
		x[0] = x[1];
		x[1] = temp;
		leftRotate ( &x[1], --length );
	}
}


Ok, I'm closer.
My rotation is still a little off. How do i recall the stored temp value into the rotated array list?

The minimum index value is being returned, but it is coming back as the next empty index.
Last edited on
I just ran your program, look at the results:

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:

1
2
3
4
int a, b;
int c = a*b;
a = 4;
b = 5;


That wouldn't work out to 20 ^^
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
90
91
#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;
float average;
int mylist [MAXSIZE];

int main ( void )
{
    
    getlist (mylist, length);
    putlist (mylist, length);
    meanAverage (mylist, length);
    
                cout << "The average is " << average << endl;
    
    minIndex (mylist, length);
                
                cout << "The minimum value index is " << minIndex (mylist, length)  << endl;
                
    leftRotate (mylist, length);
               
               for(int i=0; i < mylist [i]; ++i)
		       cout << i << " " << mylist[i] << endl;   
               
      
    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 < numels; i++)
        if (min > mylist[i])
        min = mylist[i];
    
    return min;

}
void leftRotate (int x[], int length)
{
	int temp;
	if (length == 0);
	else
    {
		temp = x[0];
		x[0] = x[1];
		x[1] = temp;
		leftRotate ( &x[1], --length );
	}
}


thanks for helping me mcleano.

I put up my revised code. I'm geeting an overload value if i try to force finding index (length)
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.
sorry, the code i put up returns the minimum value, but not it's index.

also, can you point me in the right direction for recalling the stored temp value in the rotation?
Last edited on
sorry, the code i put up returns the minimum value, but not it's index.


then just change the function type to void and instead of return min; have:

1
2
cout << min << ' ' << i; 
return; // to return to caller 


In what way do you need o recall it? for what reason?
Last edited on
Topic archived. No new replies allowed.