I'm brand new to C++ and I'm having a difficult time. I'm working on a project for school and I'm very confused with functions all together. How do I get my arrays from main so I don't have to have them in each function and how do I call each array seperately. Any insight that might get me on the right track would be appreciated.
close_encounter 0 Newbie Poster
//Author: Spencer Hitt
//Source File: Proj5code.cpp
//Description: The Stock Problem
//Compiler used: Visual Studio C++ Express Edition
#include <iostream>
using namespace std;
void displayPresent();
double findMax();
double findMin();
double calcAvg( double array1[], int count);
int SIZE = 20;
int SIZE2 = 8;
int main()
{
double stock1[] = {34.25,40.50,36.50,40.00,
30.25,30.25,35.50,36.00,
34.25,37.00,34.00,35.00,
36.25,34.25,40.50,41.50,
41.50,40.00,36.50,34.50};
double stock2[] = {40.25,38.50,34.50,33.50,
30.50,29.75,37.50,36.00,
34.75,38.00,34.25,37.00,
34.25,37.50,34.50,38.50,
37.50,37.25,38.25,37.50};
long double stock3[] = {100.41, 90.45, 99.30, 102.99,
98.54, 95.30, 92.32, 110.88};
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
displayPresent();
cout << "\n Press Enter to continue..." <<endl;
cin.ignore();
findMax();
cout << "\n Press Enter to continue..." <<endl;
cin.ignore();
findMin();
cout << "\n Press Enter to continue..." <<endl;
cin.ignore();
calcAvg(stock1, (sizeof stock1)/(sizeof stock1[0]));
cout << "\n Press Enter to continue..." <<endl;
cin.ignore();
return 0;
}
double findMax()
{
int i;
double max1;
double max2;
double max3;
double stock1[] = {34.25,40.50,36.50,40.00,
30.25,30.25,35.50,36.00,
34.25,37.00,34.00,35.00,
36.25,34.25,40.50,41.50,
41.50,40.00,36.50,34.50};
double stock2[] = {40.25,38.50,34.50,33.50,
30.50,29.75,37.50,36.00,
34.75,38.00,34.25,37.00,
34.25,37.50,34.50,38.50,
37.50,37.25,38.25,37.50};
long double stock3[] = {100.41, 90.45, 99.30, 102.99,
98.54, 95.30, 92.32, 110.88};
max1 = stock1[0];
max2 = stock2[0];
max3 = stock3[0];
for(i = 1; i < SIZE; i++)
{
if(stock1[i] > max1)
max1 = stock1[i];
if(stock2[i] > max2)
max2 = stock2[i];
}
cout << " The maximum number in stock1 = " << max1 <<endl;
cout << " The maximum number in stock2 = " << max2 <<endl;
for(i = 1; i < SIZE; i++)
{
if(stock3[i] > max3)
max3 = stock3[i];
}
cout << " The maximum number in stock3 = " << max3 <<endl;
return 0;
}
double findMin()
{
int i = 0;
double min1;
double min2;
double min3;
double stock1[] = {34.25,40.50,36.50,40.00,
30.25,30.25,35.50,36.00,
34.25,37.00,34.00,35.00,
36.25,34.25,40.50,41.50,
41.50,40.00,36.50,34.50};
double stock2[] = {40.25,38.50,34.50,33.50,
30.50,29.75,37.50,36.00,
34.75,38.00,34.25,37.00,
34.25,37.50,34.50,38.50,
37.50,37.25,38.25,37.50};
long double stock3[] = {100.41, 90.45, 99.30, 102.99,
98.54, 95.30, 92.32, 110.88};
min1 = stock1[0];
min2 = stock2[0];
min3 = stock3[0];
for(i = 0; i < 20 ; i++)
{
if(stock1[i] < min1)
min1 = stock1[i];
if(stock2[i] < min2)
min2 = stock2[i];
}
for(i = 0; i < 8 ; i++)
{
if(stock3[i] < min3)
min3 = stock3[i];
}
cout << " The minimum number in stock1 = " << min1 <<endl;
cout << " The minimum number in stock2 = " << min2 <<endl;
cout << " The minimum number in stock3 = " << min3 <<endl;
return 0;
}
double calcAvg(double array1[], int count)
{
int i = 0;
double sum = 0;
for(int i = 0 ; i < count ; i++)
{
sum += array1[i];
}
cout << " The average number of stock1 = " << sum/count <<endl;
return 0;
}
void displayPresent()
{
cout << "\n The Stock Problem" << endl << endl;
}
Banfa 597 Posting Pro Featured Poster
You can't pass an actual array to a function but what you can do is pass a pointer to the first element of the array. In many ways a pointer to the first element can be used as an actual array for example
int array[5] = {5, 4, 3, 2, 1};
int* pointer = array;
cout << array[3] << ":" << pointer[3] << endl;
would output "2 : 2".
The important thing to remember is that when you pass an array to a function like this you loose all information about the size of the array so if it is important that the function knows that it has to be passed as well
void function2(int *ptr, int size);
void function1(void)
{
int array[5] = {5, 4, 3, 2, 1};
function2(array, sizeof array/sizeof array[0]);
// array now contains 5, 4, 3, 2, 6
}
void function2(int *ptr, int size)
{
// Set the last element of the passed array to value 6
if (size > 0)
{
ptr[size-1] = 6;
}
}
Banfa 597 Posting Pro Featured Poster
Sorry just saw the code, with your findMin and findMax functions you want to pass and work on a single array, like calcAvg does. The pass back the result as the return value. Don't try and print out in the functions print out in main where the functions are called.
This is generally good advice if your functions that do the calculations also do the printing then you can not reuse them in places that just require the return value. Separate the code that performs tasks from the code than prints results putting them in different functions.
close_encounter 0 Newbie Poster
Thanks alot that makes much more sense than my book. I'll start tinkering.
close_encounter 0 Newbie Poster
This is really horrible code but this is what I'm trying to accomplish I just don't know how to return all the values to main then only call each one specifically. We haven't went over pointers so I can't use them.
//Compiler used: Visual Studio C++ Express Edition
#include <iostream>
using namespace std;
//Display the title then when the user presses 'ENTER' display
//each report accordingly.
void displayPresent();
double findMax(double[], int);
double findMin(double[], int);
double calcAvg(double[]);
int SIZE = 20;
int SIZE2 = 8;
int main()
{
double max;
double min;
double avg;
double stock1[] = {34.25,40.50,36.50,40.00,
30.25,30.25,35.50,36.00,
34.25,37.00,34.00,35.00,
36.25,34.25,40.50,41.50,
41.50,40.00,36.50,34.50};
double stock2[] = {40.25,38.50,34.50,33.50,
30.50,29.75,37.50,36.00,
34.75,38.00,34.25,37.00,
34.25,37.50,34.50,38.50,
37.50,37.25,38.25,37.50};
double stock3[] = {100.41, 90.45, 99.30, 102.99,
98.54, 95.30, 92.32, 110.88};
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
displayPresent();
//Report 1
//to output the min and max of arrays stock1, and stock2.
cout << "\n Press Enter to continue..." <<endl;
cin.ignore();
max = findMax(stock1, SIZE);
cout << max;
min = findMin(stock1, SIZE);
cout << min;
max = findMax(stock2, SIZE);
cout << max;
min = findMin(stock2, SIZE);
cout << min;
//Report 2
//to output the average of arrays stock1 and stock2 and the number of days(integers) that exceed the average.
cout << "\n Press Enter to continue..." <<endl;
cin.ignore();
avg = calcAvg(stock1);
cout << avg;
avg = calcAvg(stock2);
cout << "\n Press Enter to continue..." <<endl;
cin.ignore();
//Report 3
//to ouput the max of array stock3 and the average.
max = findMax(stock3, SIZE);
cout << max;
avg = calcAvg(stock3);
return 0;
}
double findMax(double[], int)
{
int i;
double max1;
double max2;
double max3;
double stock1[] = {34.25,40.50,36.50,40.00,
30.25,30.25,35.50,36.00,
34.25,37.00,34.00,35.00,
36.25,34.25,40.50,41.50,
41.50,40.00,36.50,34.50};
double stock2[] = {40.25,38.50,34.50,33.50,
30.50,29.75,37.50,36.00,
34.75,38.00,34.25,37.00,
34.25,37.50,34.50,38.50,
37.50,37.25,38.25,37.50};
double stock3[] = {100.41, 90.45, 99.30, 102.99,
98.54, 95.30, 92.32, 110.88};
max1 = stock1[0];
max2 = stock2[0];
max3 = stock3[0];
for(i = 0; i < SIZE; i++)
{
if(stock1[i] > max1)
max1 = stock1[i];
if(stock2[i] > max2)
max2 = stock2[i];
}
//cout << " The maximum number in stock1 = " << max1 <<endl;
//cout << " The maximum number in stock2 = " << max2 <<endl;
for(i = 0; i < SIZE; i++)
{
if(stock3[i] > max3)
max3 = stock3[i];
}
//cout << " The maximum number in stock3 = " << max3 <<endl;
return 0;
}
double findMin(double[], int)
{
int i = 0;
double min1;
double min2;
double min3;
double stock1[] = {34.25,40.50,36.50,40.00,
30.25,30.25,35.50,36.00,
34.25,37.00,34.00,35.00,
36.25,34.25,40.50,41.50,
41.50,40.00,36.50,34.50};
double stock2[] = {40.25,38.50,34.50,33.50,
30.50,29.75,37.50,36.00,
34.75,38.00,34.25,37.00,
34.25,37.50,34.50,38.50,
37.50,37.25,38.25,37.50};
double stock3[] = {100.41, 90.45, 99.30, 102.99,
98.54, 95.30, 92.32, 110.88};
min1 = stock1[0];
min2 = stock2[0];
min3 = stock3[0];
for(i = 0; i < SIZE ; i++)
{
if(stock1[i] < min1)
min1 = stock1[i];
if(stock2[i] < min2)
min2 = stock2[i];
}
for(i = 0; i < SIZE2 ; i++)
{
if(stock3[i] < min3)
min3 = stock3[i];
}
// cout << " The minimum number in stock1 = " << min1 <<endl;
//cout << " The minimum number in stock2 = " << min2 <<endl;
// cout << " The minimum number in stock3 = " << min3 <<endl;
return 0;
}
double calcAvg(double[])
{
int i = 0;
int j = 0;
double sumStock1 = 0;
double sumStock2 = 0;
double sumStock3 = 0;
int numofdaysover = 0;
double stock1[] = {34.25,40.50,36.50,40.00,
30.25,30.25,35.50,36.00,
34.25,37.00,34.00,35.00,
36.25,34.25,40.50,41.50,
41.50,40.00,36.50,34.50};
double stock2[] = {40.25,38.50,34.50,33.50,
30.50,29.75,37.50,36.00,
34.75,38.00,34.25,37.00,
34.25,37.50,34.50,38.50,
37.50,37.25,38.25,37.50};
double stock3[] = {100.41, 90.45, 99.30, 102.99,
98.54, 95.30, 92.32, 110.88};
for(int i = 0 ; i < SIZE ; i++)
{
sumStock1 += stock1[i];
sumStock2 += stock2[i];
}
for(int i = 0; i < SIZE2 ; i++)
{
sumStock3 += stock3[i];
}
for(int j = 0; j < SIZE; j++)
{
if((sumStock1/SIZE)< stock1[j])
numofdaysover++;
}
cout << " The average of stock1 = " << sumStock1/SIZE << " # of days exceeded = " << numofdaysover <<endl;
for(int j = 0; j < SIZE; j++)
{
if((sumStock2/SIZE)< stock2[j])
numofdaysover++;
}
cout << " The average of stock2 = " << sumStock2/SIZE << " # of days exceeded = " << numofdaysover <<endl;
for(int j = 0; j < SIZE; j++)
{
if((sumStock2/SIZE)< stock3[j])
numofdaysover++;
}
cout << " The average of stock3 = " << sumStock3/SIZE << " # of days exceeded = " << numofdaysover <<endl;
return 0;
}
void displayPresent()
{
cout << "\n The Stock Problem" << endl << endl;
}
Banfa 597 Posting Pro Featured Poster
For what you need to do you can treat the pointer as an array, pointer[0] will work as if pointer was actually an array. Your definitions should look like this
double findMax(double array[], int size)
{
double max;
// Code here working on array and size to calculate max
return max;
}
Delete the array stock1, stock2 and stock3 from the inside of findMax instead do the same operations on array to calculate the maximum value in the array which you can then return.
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.