Hey!! I'm new to C++, this is the final program of my semester and I am stuck sooooo hard...

I have the code laid out and it will compile but my functions wont work like I want them too. here is the code:

#include<iostream>

using namespace std;


void getlist(int[], int&);

void putlist (const int[], int);

float mean_average (const int[], int);

int MinIndex (const int[], int);

void left_rotate (int[], int);



const int MAXSIZE = 50;


int main()

{
int mylist[MAXSIZE];
int num_items;

    
    getlist (mylist, num_items);
    putlist (mylist, num_items);
    MinIndex (mylist, num_items);
    mean_average(mylist, num_items);
    left_rotate (mylist, num_items);
    
    
    system ("PAUSE");
    return 0;
}

void getlist(int mylist[], int &num_items)    
{
    
    cout << "Enter the number of Array Elements, Maximum " << MAXSIZE << endl;
    cin >> num_items;
    
    for (int i = 0; i < num_items; i ++)
        {
        cout << "Enter the next array element\n";
        cin >> mylist[i];
        }
}

void putlist (const int mylist[], int num_items)
{    
    cout << "Array elements\n";
    for (int i = 0; i < num_items; i ++)
        {
    cout << i << " " << mylist [i] << endl;
         }
}    
int MinIndex (const int mylist[], int num_items)
{
                    int min =mylist[0];
               
               for(int i = 0; i < num_items; i++)
                       {
                        if (min <= mylist[i])
                        min = i;
                       }
                       
			cout<< "The address of the smallest number is "<< min<<endl;
		 return min;
}
float mean_average(const int mylist[],int num_items)
{
      int sum=0;
      float average;
      
      
             
	  for(int i= 0; i <= num_items; i++)
	  {
	  sum= sum + mylist[i];
      }
      		average= sum/num_items;
       
	cout<< "The average value is: "<<average<<endl; 
	 return average;

}
void left_rotate (int mylist[], int num_items)
{
	int temp = mylist[0]; 
		for(int i = 0; i < num_items-1; i++)
		{
                cout << "The new array elements are:\n";
           	    mylist[i] = mylist[i+1]; 
                
                cout << i << " " << mylist [i]<<endl;
                mylist[num_items-1] = temp;
                cout<<temp;
		}		
}

The array left rotate does rotate but the value stored in temp wont appear, and the calculations for MinIndex and mean_average are just not working out.... I've looked all over the web, but even when I find codes that are identical they still don't work. what am I doing wrong?? I would write it simpler but the format is require and all the functions were dictated so I cant alter them. This is due tuesday- please help!!

Recommended Answers

All 3 Replies

For the most part the code is pretty good. You made a few minor errors that would be expected since you are still starting out but you should really work on consistency in indentation since it is harder to read and edit (you will really notice this later on) so you should get into the habit of trying to make your code look nice early on.


I commented in a few places where I made changes.

#include <iostream>
#include <cstdlib> //needed for system() because not all compilers include this header by default

using namespace std;

//the way you are using your functions you can/should have them all return void
void getlist(int[], int&);
void putlist (const int[], int);
void mean_average (const int[], int);
void MinIndex (const int[], int);
void left_rotate (int[], int);

const int MAXSIZE = 50;

int main()
{
	int mylist[MAXSIZE];
	int num_items;

	getlist(mylist, num_items);
	putlist(mylist, num_items);
	MinIndex(mylist, num_items);
	mean_average(mylist, num_items);
	left_rotate(mylist, num_items);

	system ("PAUSE");
	return 0;
}

void getlist(int mylist[], int &num_items)
{
	cout << "Enter the number of Array Elements, Maximum " << MAXSIZE << endl;
	cin >> num_items;

	for( int i = 0; i < num_items; i++ )
	{
		cout << "Enter the next array element: ";
		cin >> mylist[i];
	}
	cout << endl;
}

void putlist(const int mylist[], int num_items)
{
	cout << endl << "Array elements\n";
	for( int i = 0; i < num_items; i++ )
	{
		cout << i << " " << mylist [i] << endl;
	}
}

void MinIndex(const int mylist[], int num_items)
{
	int min = 0; //assuming min index is 0 (not the actual value there)

	for( int i = 0; i < num_items; i++ )
	{
		if( mylist[i] < mylist[min] ) //compare the values at each index
			min = i;
	}

	cout << endl << "The address of the smallest number is " << min << endl;
}

void mean_average(const int mylist[],int num_items)
{
	int sum = 0;
	float average;

	for( int i= 0; i < num_items; i++ ) //use < if your are using num_items or <= if you use num_items-1
	{
		sum += mylist[i];
	}
	average = sum / num_items;

	cout << endl << "The average value is: " << average << endl;
}

//used a bit of your code but rewrote most of left_rotate (this seems more like a left shift)
void left_rotate(int mylist[], int num_items)
{
	int temp = mylist[0];
	for( int i = 0; i < num_items-1; i++ )
	{
		mylist[i] = mylist[i+1];
	}
	mylist[num_items-1] = temp;

	cout << endl << "The new array elements are:\n";
	for( int i = 0; i < num_items; i++ )
		cout << mylist[i] << " ";
	cout << endl << endl;
}

In mean_average

for(int i= 0; i <= num_items; i++)

You want to loop until less than num_items or else you will walk off the array.

Thank you guys!!! I've been tweaking this for 8 hours now... You have helped immensely!! Thank you for the fast response too!! If you were here, and you drank, I'd definitely buy you all a round! Oh and sorry bout the indents- for some reason DevC++ and I are not having luck getting the indents to match. Microsoft Visual seems to accept the indents better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.