GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Jan-2005, 00:57
Parkin_m Parkin_m is offline
New Member
 
Join Date: Jan 2005
Posts: 4
Parkin_m is on a distinguished road
Smile

Help passing array data to other function.


Hello, My name is michael i am 19 and studying a degree in audio technology. Im working on c++ atm and am having a bit of a problem! Hope you can help!
This is a basic program i have written which asks the user for 5 midi note values. these are then stored in an array and then used to find the highest value. I now wish to put the highest finiding part of my code into a seperate function, can someone help me as i havent got a clue where to start! I can send data to a function, but cant work out how to send array infomation and then return it. If you could explain simply and step by step it would be most appreciated as im new to c++ and i want to learn from the basics. Thank you.
Mike

CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>



float midi_note[5];
int index=0;
float highest=0; 

void main (void)


/*Gets the user to input 5 midinotes, and stores them in an array.*/
{

	for (index=0; index<5; index++)
	{				
		cout<<"Please enter midi note "<<index + 1<<endl;
		cin>>midi_note[index];
				
		
/*If the midi note entered is less than 0 or greater than 127 user promted to re-enter midi note.*/
		
		if (midi_note[index] < 0 || midi_note[index] > 127)
		{
			cout<< "number incorrect, must be between 0 and 127"<<endl;
			index--;
		}
	}


	
	
/*this assigns the float highest the value stored within midi_note[0]
it then checks all the other midi_note values and if any are higher than highest, assigns this value.
This means that the float highest will be left with the biggest midi_note value and can then be displayed.*/
	
	
	highest=midi_note[0];

	for (index = 0; index < 5; index++)
	{

		if (midi_note[index] > highest)
		{
			highest = midi_note[index];
		}
	}
		

	cout<<"The highest midi note you have entered is "<<highest<<endl;

}
  #2  
Old 19-Jan-2005, 03:39
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
#1) don't use iostream.h use
CPP / C++ / C Code:
#include <iostream>
using namespace std;
the .h versiion is very old and has been replaced with other style.

#2) main() is always and int, never a void

#3)
CPP / C++ / C Code:
{
...
    highest = getmax(midi_note, 5);  // call the routine passing in the array and it's size
...
}

// getmax returns a float 
// *notes is the array midi_note
// the * denotes the address of the array
float getmax(float *notes, int sz)  
{
    float max;
    int ind;

    max = notes[0];
    for (ind=1; ind < sz; ind++)
    {
        if (notes[ind] > max)
        {
            max = notes[ind];
        }
    }

    return max;    // return the value to the calling program
}
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 19-Jan-2005, 12:05
Parkin_m Parkin_m is offline
New Member
 
Join Date: Jan 2005
Posts: 4
Parkin_m is on a distinguished road
Thank you very much for such a quick reply, however i am still confused as to how to lay the function out in relation to my piece of coding, i dont want to ask you to do it for me, but i am still stuck. I have attempted to write the piece of code, but i have gotten bits wrong.

coulu you correct this for me in full? then i can break it down and understand how it works.

thank you again!

CPP / C++ / C Code:
#include <iostream>
using namespace std;

// function prototype
int getmax(float *notes, int sz);
 

//main function
int main (int)
{
	float midi_note[5];
	int index=0;
	int highest=0;
	


	for (index=0; index<5; index++)
	{				
		cout<<"Please enter midi note"<<endl;
		cin>>midi_notes[index];
	}

	cout << highest = getmax( *notes, 5);

}


//getmax function
float getmax(float *notes, int sz);
{
    float max;
    int ind;

    max = notes[0];
    for (ind=1; ind < sz; ind++)
    {
        if (notes[ind] > max)
        {
            max = notes[ind];
        }
    }

    return max;    // return the value to the calling program
}
  #4  
Old 19-Jan-2005, 12:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Parkin_m
Thank you very much for such a quick reply, however i am still confused as to how to lay the function out in relation to my piece of coding, i dont want to ask you to do it for me, but i am still stuck. I have attempted to write the piece of code, but i have gotten bits wrong.

coulu you correct this for me in full? then i can break it down and understand how it works.

thank you again!



I added a couple of comments:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

// function prototype
float getmax(float *notes, int sz); // return type must be same as function
 

//main function // is this comment really necessary? (There is only one main())
int main (int)
{
  float midi_note[5];
  int index;
  float highest; // since you are comparing floats
  


  for (index=0; index<5; index++)
  {        
    cout<<"Please enter midi note"<<endl;
    cin>>midi_note[index]; // array name is midi_note
  }

  highest = getmax(midi_note, 5); //use array name (it's used as pointer to float)
  cout << "highest = " << highest << endl; // tell what is being printed

}


//getmax function // no useful information in the comment: why bother?
float getmax(float *notes, int sz) // no semi-colon here
{
    float max;
    int ind;

    max = notes[0];
    for (ind=1; ind < sz; ind++)
    {
        if (notes[ind] > max)
        {
            max = notes[ind];
        }
    }

    return max;    // return the value to the calling program ( another superfluous comment?)
}


Regards,

Dave
  #5  
Old 19-Jan-2005, 14:48
Parkin_m Parkin_m is offline
New Member
 
Join Date: Jan 2005
Posts: 4
Parkin_m is on a distinguished road
Wicked thanks alot. The comments werent final comments, i just put them in so i could sort the code out in my head! Im a newbie so it was helping me to seperate the differnent functions, your right tho they arent really needed!

I wonder if someone could point me in the right direction of thinking for my next problem.?
I want to now include another function, which outputs every midinote with a value of 4. I can do this in the main block of code by doing the following...

CPP / C++ / C Code:
for (index=0; index<5; index++)
	{
		if (midi_note[index] == 4)
		{
			cout<<"midi note "<<index<<" was equal to 4"<<endl;
		}
		else
		{
			cout<<"midi note "<<index<<" was not equal to 4"<<endl;
		}
	}

I am getting confused as i cant work out what i will need to return, and which parts are getting sent to which bits! any help would be most appreciated

Thankyou Michael
  #6  
Old 19-Jan-2005, 15:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Parkin_m
Wicked thanks alot. The comments werent final comments, i just put them in so i could sort the code out in my head! Im a newbie so it was helping me to seperate the differnent functions, your right tho they arent really needed!

I wonder if someone could point me in the right direction of thinking for my next problem.?
I want to now include another function, which outputs every midinote with a value of 4. I can do this in the main block of code by doing the following...

CPP / C++ / C Code:
for (index=0; index<5; index++)
	{
		if (midi_note[index] == 4)
		{
			cout<<"midi note "<<index<<" was equal to 4"<<endl;
		}
		else
		{
			cout<<"midi note "<<index<<" was not equal to 4"<<endl;
		}
	}

I am getting confused as i cant work out what i will need to return, and which parts are getting sent to which bits! any help would be most appreciated

Thankyou Michael


If you want a function just to print out certain things, it doesn't have to return anything. (In other languages that is called a procedure, not a function, but in C there are only functions.)

A function that doesn't return anything is given the type void

CPP / C++ / C Code:
void PrintIfFour(int *notes, int sz)
{
  // Put stuff here to print results or whatever you want it to do

  // You don't need a "return" statement, since you don't return
  // a value to the calling program.

  return; // It's ok to have this here, but not necessary
}

Where the function will be prototyped exactly as written:

CPP / C++ / C Code:
int getnam (int *notes, int sz);
int PrintIfFour(int *notes, int sz);
.
.

Note that I changed things so that it is working on an array of int, not float. Use whatever you need the midi_note thingies to be, but be aware that if you are using floats, not all values may be represented exactly, and comparisons may not be valid (due to roundoff error). If you don't need floats, use ints.

Regards,

Dave
  #7  
Old 19-Jan-2005, 17:51
Parkin_m Parkin_m is offline
New Member
 
Join Date: Jan 2005
Posts: 4
Parkin_m is on a distinguished road
Thumbs up

Thank you very very much, everyone who has helped! :-)

Mike
  #8  
Old 19-Jan-2005, 18:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by davekw7x

CPP / C++ / C Code:
void PrintIfFour(int *notes, int sz)
{
  // Put stuff here to print results or whatever you want it to do

  // You don't need a "return" statement, since you don't return
  // a value to the calling program.

  return; // It's ok to have this here, but not necessary
}

Where the function will be prototyped exactly as written:

CPP / C++ / C Code:
int getnam (int *notes, int sz);
int PrintIfFour(int *notes, int sz); /* <=== edit: wrong, should be void type */
.
.



Note the error in my example snippit! Make the function declaration look like the prototype:

CPP / C++ / C Code:
void PrintIfFour(int *notes, int sz);

Sorry 'bout that.

Regards,

Dave
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 09:48
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 05:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.