Functions

Add functions to my problem.

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 2190 Views - Last Post: 28 November 2007 - 07:18 AM Rate Topic: -----

#1 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Functions

Post icon  Posted 20 November 2007 - 05:52 AM

My teacher said she wanted functions added to my problem. I have no clue what they should be can you look at it.

Thanks.
Cooplis :D
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <conio.h>

void high_low(double, double);
double arr1 = 20;
double arr3 = 8;	
void bubble_srt(double stock1[], double n);

using namespace std;

int main() 
{
	int index;
	int size;
	double Avg1;
	double Avg2;
	int index_of_min;
	int number_used;
	
	
	double stock1[20] = {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[20]=  {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[8]= {100.41, 90.45, 99.30, 102.99,
					  98.54, 95.30, 92.32, 110.88};													  

//Report 1 - show highest and lowest numbers in array	
{
cout << "				   REPORT 1				 \n\n";
	sort(stock1, stock1+20);
	sort(stock2, stock2+20);
	{
	for (int i=0; i<1; i++) {
	   cout << "The highest number in Stock 1 is " << stock1[19] <<" \n";
	   cout << "The lowest number in Stock 1 is " << stock1[0]  <<" \n\n\n"; 
	   cout << "The highest number in Stock 2 is " << stock2[19] <<" \n";
	   cout << "The lowest number in Stock 2 is " << stock2[0]  <<" \n\n\n";
}   
system("pause\n");	
//Report 2 - The average of each set of 20 stocks and the number of days, 
//on which each stock price exceeded its average price.
{ 
cout << "\n\n				 REPORT 2				 \n\n";   
	 int count = 0;
	 int  count1=0; 
   {
	 
	int j = 20;
	  for (j = 0; j < 20; j++)
		   if(stock1[j] > stock2[j]) ++count;
		  
	  int k = 20;
	  for (k = 0; k < 20; k++)
		   if(stock2[k] < stock1[k]) ++count1;
		}
		cout << "Number of days Stock1 exceeded Stock2: "<<count;
		cout << "\n";
		cout << "Number of days Stock2 exceeded Stock1: "<<count1;	
		cout << "\n\n";		  
	   cout << "Number in Array: \n"<< arr1;
		   {
	   double sum = 0;
	   double average = 0;
	 for (int i = 0; i < 20; ++i)
		sum+=stock1[i];
	average = sum/arr1;
	cout<<"Stock1 Average:"<<average;
	cout << "\n\n";  
}
}
	double sum = 0;
	double average1 = 0;
	 for (int i = 0; i < 20; ++i)
		sum+=stock2[i];
	average1 = sum/arr1;
	cout<<"Stock2 Average:"<<average1;
	cout << "\n\n";
		  }  
		  }
system("pause");
		


//Report 3 - Using the functions from Reports 1 and 2, find the lowest 
//and highest price in the stock3 array, and find the average of the stocks in 
//the following array of 8 prices above.
//Sort the stock3 array and display in ascending order with this report


cout << "\n\n				  REPORT 3		\n\n";
{
	 int i;
	 
	sort(stock3, stock3+8);
	for (int i=0; i<1; i++) {
		cout << "The highest number in Stock 3 is " << stock3[7] <<" \n";
	   cout << "The lowest number in Stock 3 is " << stock3[0]  <<" \n\n\n";
	   }
   }	
system("pause");


//Report 4 - The 16 five-day moving averages for stock1 and stock2 with 
//appropriate headings.  Moving average means find the average for prices 1 to 5 
//in a stock list and then the average for prices 2 to 6, etc.
cout << "\n\n				  REPORT 4		\n\n";
void bubble_srt(double, double);
{
	   int i;
	  
   cout << "Before the sort:\n";	 // Show array elements before sort
   for(i = 0; i < 19; i++)
	cout << stock2[i]<< "\n";
  cout << " \n";
}
void bubble_srt(double stock1, double arr1);		// Sort the array
   int i;
   {
	 printf("After the sort:\n");	 // Show results after the sort
	 for(i = 0; i < 20; i++)
	  cout << stock2[i]<< "\n";
	  cout<<("\n");   
}
system("PAUSE");  
	 } 
				 // Pause the screen 

				 


This post has been edited by cooplis: 20 November 2007 - 05:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Functions

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Functions

Posted 20 November 2007 - 06:30 AM

I'd suggest that function for each report would be logical. You already have them separated into sections in the code, so just make them functions.
Was This Post Helpful? 0
  • +
  • -

#3 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Re: Functions

Posted 20 November 2007 - 06:42 AM

View PostAmadeus, on 20 Nov, 2007 - 06:30 AM, said:

I'd suggest that function for each report would be logical. You already have them separated into sections in the code, so just make them functions.

I had it listed as report1, report2 and so on. But she said that's not what she wanted. I guess my understanding of the functions is not clear if I change the program, I am just adding a pointer to each section in the main right?
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: Functions

Posted 20 November 2007 - 07:31 AM

You have a stock1 and stock2, to which you do identical operations. I would guess they're looking for you to recognize this and write less code. ;)

For example:
void showHiLo(char *name, double stock[], int stockLen) {
   sort(stock, stock+20);
   cout << "The highest number in " << name << " is " << stock[stockLen-1] <<" \n";
   cout << "The lowest number in " << name << " is " << stock[0]  <<" \n\n\n";
}
...
   cout << "				   REPORT 1				 \n\n";
   showHiLo("Stock 1", stock1, 20);
   showHiLo("Stock 2", stock2, 20);
...



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Re: Functions

Posted 20 November 2007 - 08:19 AM

View Postbaavgai, on 20 Nov, 2007 - 07:31 AM, said:

You have a stock1 and stock2, to which you do identical operations. I would guess they're looking for you to recognize this and write less code. ;)

For example:
void showHiLo(char *name, double stock[], int stockLen) {
   sort(stock, stock+20);
   cout << "The highest number in " << name << " is " << stock[stockLen-1] <<" \n";
   cout << "The lowest number in " << name << " is " << stock[0]  <<" \n\n\n";
}
...
   cout << "				   REPORT 1				 \n\n";
   showHiLo("Stock 1", stock1, 20);
   showHiLo("Stock 2", stock2, 20);
...



Hope this helps.

Ok I want to understand the function, I don't get it I did functions once before this looks a little different. I went back to my program and tried to incorporate a similar function but it is not coming out right cause I understand what's what I guess. For instance Char *name is a reference to stock?
Was This Post Helpful? 0
  • +
  • -

#6 curiose  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

Re: Functions

Posted 20 November 2007 - 11:15 AM

Hello....i will add to you the same example that i used with a member here... actually i like it because i usnderstood functions from it simpley..
first functions mean:
is as a particular activity usually used to accomplish a specific set of objectives.
Function should follow three rules at atime:
1-Declared (PROTOTYPED)
2-Defined
3-Invoked (Function Calling)
and it has 3 Characteristics:
Function name
Parameters
Return Type
this an axample:
#include <stdio.h>
int main()
{
void message();//declaration
printf ("main function!");
message();//calling
return 0;
}

	void message()//definition
	{
	printf("\n message function");
	}


the techniuqe to use the same values in two functions and more is like this
#include <stdio.h>
int main()
{
int add (int x, int y);//this x&y only for add function and here we declare
int x, y, result;
printf ("Please enter two numbers::");
scanf ("%d %d", &x, &y);
result=add (x, y);//the calling 

printf("\n The result is %d", result);
return 0;
}

int add (int x, int y)//definition
{
int res;
res=x + y;
return (res);//return the result to the main and display the result in it
}


hope that helps ...try to use functions from entering the required variables into it inside the declaration,calling and by using new name foro them inside the new function
Was This Post Helpful? 0
  • +
  • -

#7 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Re: Functions

Posted 20 November 2007 - 11:19 AM

View Postcuriose, on 20 Nov, 2007 - 11:15 AM, said:

Hello....i will add to you the same example that i used with a member here... actually i like it because i usnderstood functions from it simpley..
first functions mean:
is as a particular activity usually used to accomplish a specific set of objectives.
Function should follow three rules at atime:
1-Declared (PROTOTYPED)
2-Defined
3-Invoked (Function Calling)
and it has 3 Characteristics:
Function name
Parameters
Return Type
this an axample:
#include <stdio.h>
int main()
{
void message();//declaration
printf ("main function!");
message();//calling
return 0;
}

	void message()//definition
	{
	printf("\n message function");
	}


the techniuqe to use the same values in two functions and more is like this
#include <stdio.h>
int main()
{
int add (int x, int y);//this x&y only for add function and here we declare
int x, y, result;
printf ("Please enter two numbers::");
scanf ("%d %d", &x, &y);
result=add (x, y);//the calling 

printf("\n The result is %d", result);
return 0;
}

int add (int x, int y)//definition
{
int res;
res=x + y;
return (res);//return the result to the main and display the result in it
}


hope that helps ...try to use functions from entering the required variables into it inside the declaration,calling and by using new name foro them inside the new function

void sort_stock();
void sort_stock(char *name, double stock[], int stockLen);
sort_stock(stock, stock+20);
	sort(stock1, stock1+20);
	for (int i=0; i<1; i++) {
	   cout << "The highest number in Stock 1 is " <<  stock1[19] <<" \n";
	   cout << "The lowest number in Stock 1 is " <<   stock1[0]  <<" \n\n\n"; 

This is not working for me

This post has been edited by cooplis: 20 November 2007 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#8 curiose  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 99
  • Joined: 28-October 07

Re: Functions

Posted 20 November 2007 - 11:31 AM

ok not aproblem you will find the answer soon
Was This Post Helpful? 0
  • +
  • -

#9 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Re: Functions

Posted 20 November 2007 - 12:31 PM

View Postcuriose, on 20 Nov, 2007 - 11:31 AM, said:

ok not aproblem you will find the answer soon



So I am warm?
Was This Post Helpful? 0
  • +
  • -

#10 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Re: Functions

Posted 27 November 2007 - 07:40 AM

Check this out what's wrong with it? This is my function for my average.


void Avg_stk(double average, double average0, int i);
Avg_stk(average, average0, i);
return 0;	 
void Avg_stk(double average, double average0, int i);	   
	  
	   double sum = 0;
	   double average = 0;
	   double average0 = 0;
	  

	 for (int i = 0; i < 20; ++i)
		sum+=stock1[i]; 
		 sum+=stock2[i];
	average = sum/arr1;
	cout<<"\n\nStock1 Average:   "<<   average;
	cout << "\n\n"; 
	 cout<<"\nStock2 Average:   "<< average0;
	cout << "\n\n";
  }


Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Functions

Posted 27 November 2007 - 08:01 AM

Why do you think there's something wrong? Error message? Incorrect result? Can you post your whole code? the snippet provided is not even in proper function syntax, and your for loop will execute only the line that immediately follows it.
Was This Post Helpful? 0
  • +
  • -

#12 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Re: Functions

Posted 27 November 2007 - 08:04 AM

View PostAmadeus, on 27 Nov, 2007 - 08:01 AM, said:

Why do you think there's something wrong? Error message? Incorrect result?

It keeps telling me that average is undeclared
"line 70 `average' undeclared (first use this function) "

I am using the function right?
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Functions

Posted 27 November 2007 - 08:26 AM

Not really, because from what I can see you have never defined the function at all. I see two function calls, and some code that rests completely outside a function, but no function per se. A function defintion should look like this:
int myFunc(int example)
{
   int retval = 0;
   cout<<"Paramter is "<<example<<endl;
   retval=example+10;
   return retval;
}


Was This Post Helpful? 0
  • +
  • -

#14 cooplis  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 77
  • Joined: 11-September 07

Re: Functions

Posted 27 November 2007 - 08:50 AM

View PostAmadeus, on 27 Nov, 2007 - 08:26 AM, said:

Not really, because from what I can see you have never defined the function at all. I see two function calls, and some code that rests completely outside a function, but no function per se. A function defintion should look like this:
int myFunc(int example)
{
   int retval = 0;
   cout<<"Paramter is "<<example<<endl;
   retval=example+10;
   return retval;
}


Now I feel like I have too much info in my function.

double Avg_stk(double average,  double average0, double sum)
{
   double average = 0;
   double average0 = 0;
   double sum = 0;
	  
   average = sum/arr1;
   average0 = sum/arr1;
   for (int i = 0; i < 20; ++i)
   sum+=stock1[i];
   for (int j = 0; j < 20; ++j)
   sum+=stock2[j];
   cout<<"\n\nStock1 Average:   "<< average;
   cout << "\n\n"; 
   cout<<"\nStock2 Average:   "<< average0;
   cout << "\n\n";
   return 0;
}


This post has been edited by cooplis: 27 November 2007 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#15 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Functions

Posted 27 November 2007 - 10:04 AM

Again, that is not a function. that is a block of code. See my example above...the first line (the function header) should not end with a semi colon. The function should be encased in braces. And again, your for loop will run only the line immediately following it, as the body of the for loop is not encased in braces.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2