Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Stormrage

God O' God, please help thee!

This topic is 5663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

These functions and arrays! I can''t take them!! AAAAAAAAHH! Whew, that let off some steam. Okay, these functions are really weird. How do you decide when to use functions and which arguments to use?! Also I don''t understand Arrays because I don''t understand functions. Whoever has a copy of C++ For Dummies, then please go to page 87. Oh forget it, I''ll type up the code here because I''m in desperate need of help:
//ArrayDemo- demonstrate the use of arrays by reading

//a sequence of integers and then displaying them in order

#include <stdio.h>
#include <iostream.h>

//prototype declarations

int sumArray(int integerArray[], int sizeOfloatArray);
void displayArray(int integerArray[], int sizeOfloatArray);
int main(int nArg, char* pszArgs[])
{
	//input the loop count

	int nAccumulator=0;
	cout<<"This program sums values entered by the user\n";
	cout<<"Terminate the loop by entering a negative number\n";
	//store numbers into an array

	int inputValues[128];
	int numberOfValues=0;
	for(; numberOfValues < 128; numberOfValues++)
	{
		//fetch another number

		int integerValue;
		cout<<"Enter next number: ";
		cin>>integerValue;

		//if it''s negative...

		if (integerValue <0)
		{
			//...then exit

			break;
		}
		//...otherwise store the number into the storage array

		inputValues[numberOfValues]=integerValue;
	}
	   // now output the values and the sum of the values

	   displayArray(inputValues, numberOfValues);
	   cout<<"The sum is " ;
	   cout<<sumArray(inputValues, numberOfValues);
	   cout<<"\n";
	   return 0;
	}
	//displayArray-display the members of an array of length sizeOfloatArray

	void displayArray(int integerArray[], int sizeOfArray)
	{
		cout<<"The value of the array is: \n";
		for (int i=0; i< sizeOfArray; i++)
		{
			cout.width(3);
			cout <<i<< ": "<<integerArray[i] << "\n";
		}
		cout<<"\n";
	}
		//sumArray-return the sum of the members of an integer array

	int sumArray(int integerArray[], int sizeOfArray)
	{
		int accumulator =0;
		for (int i=0; i< sizeOfArray; i++)
		{
			accumulator += integerArray[i];
		}
		return accumulator;
		int L;
		cout<<"Press any key + ENTER to exit\n";
		cin>>L;
	}
Well, there it is...Someone please explain to me how the int sumArray(int integerArray[], int sizeOfloatArray) is and also what void displayArray(int integerArray[], int sizeOfloatArray) is. *Sigh* Actually just explain how this whole program works. I understand everything else in the program perfectly but these damned arrays and functions are in my way...please help

Share this post


Link to post
Share on other sites
Advertisement
Could you try rephrasing your question? Do you mean where are those functions actually defined (vs. just being declared as they are)? Or how does passing integer arrays work?

Share this post


Link to post
Share on other sites
I need to know why you use certain functions and not others and how you knwo you need to use this argument instead of that one. I don''t understand functions!

As for Arrays, I don''t understand how you get things like int sumArray(int integerArray[], int sizeOfloatArray) and void displayArray(int integerArray[], int sizeOfloatArray. I don''t understand arrays at all either. If I were to write my own little program using arrays and functions, I wouldn''t know where to start because I don''t know how you choose the right argument, etc. I can''t make a function/Array because I don''t know how to make them properly. Argh, its just all really confusing for me.

Share this post


Link to post
Share on other sites
G'day,

You usually use functions when you need to repeat a block of code more than once. Function parameters contain the information the function needs to complete its task. Not all functions require parameters.


int sumArray(int integerArray[], int sizeOfloatArray);

void displayArray(int integerArray[], int sizeOfloatArray);


These are declarations of the 2 functions your example program uses. below you will see the actual functions and how they work:


void displayArray(int integerArray[], int sizeOfArray)
{
cout<<"The value of the array is: \n";
for (int i=0; i< sizeOfArray; i++)
{
cout.width(3);
cout <<i<< ": "<<integerArray[i] << "\n"; } cout<<"\n"; }

//sumArray-return the sum of the members of an integer array int sumArray(int integerArray[], int sizeOfArray) { int accumulator =0; for (int i=0; i< sizeOfArray; i++) { accumulator += integerArray[i]; } return accumulator; int L; cout<<"Press any key + ENTER to exit\n"; cin>>L; }



in your program you call these 2 functions:

displayArray(inputValues, numberOfValues);
sumArray(inputValues, numberOfValues);

they both have different tasks, the first displays the contents of your array and the second calcualtes the sum of the array.

the first parameter (inputValues) is a pointer to your array of integers, and your second parameter (numberOfValues) is the number of integers the array is expected to have.

If you are having difficulty understanding either functions or arrays maybe you could try learning one at a time.

Hope that helps

EDIT: code tags, and whats up with the formatting? =/


[ Hazard Ball Demo | Genaside Website ]

[edited by - genaside on June 17, 2003 8:46:09 PM]

Share this post


Link to post
Share on other sites
Hmmm, where to begin. First the functions: It all depends on what you want your function to do how it will work. These functions work with the array that the program creates from your input. They need to be passed the array and the size of the array so they know what data to work with and how much there is to work with (they know nothing about what''s in your "main" program except what is passed as arguments.) Functions are used so you can write code that you might need to use several times in your program. When you call a function in your main program the program jumps to the code for your function, executes it, then jumps back to where it was in the main section of your program.

Arrays: Arrays are used to store related data. The array in your program here takes integers from the user and stores them in a single array (this way you can reference all that data with one variable name!) Arrays can be referenced by index numbers (i.e. If I have the array intArray[], then calling intArray[10] gives me the tenth value in the array).

Even this little program shows how useful arrays and functions are. It stores all the integers you input into one simple array, passes that array to a couple different functions and processes them.

Maybe I was a bit to thorough here, and maybe I just confused you even more. But I hope this helps.

"If I only had 6 months left to live I''d get back together with my first wife,
because 6 months with her would be like an eternity."
-Bobby Hall

Share this post


Link to post
Share on other sites
That helped a lot! I''ve gotten the picture of Functions/Arrays but I''m still wondering why the programmer put in the int integerArray[], int sizeOfloatArray part in parethesis.

Also, how do you put integers into your Array and then take them out when you need it again?

Thanks a lot so far!

Share this post


Link to post
Share on other sites
quote:

(i.e. If I have the array intArray[], then calling intArray[10] gives me the tenth value in the array).



Only (sometimes) in VB In C/C++ that''d be the 11th.


Mr. Nitpik,
Mark

Share this post


Link to post
Share on other sites
Good point Beos, missed that.

As for your question about taking values "out" of an array Stormrage, you don''t really. Lets say you create an array of size 10 called newarray. You can simply treat each element of the array like a normal integer variable to access it.

For instance you could put:
newarray[1] = 19; This would set the second element of the array to the number 19. (arrays are referenced starting at 0, so in this example we would go from elemnts 0 - 9).

We could also access the second element like this:
int newint;
newint = newarray[1];
This would set the integer value newint to 19!

"If I only had 6 months left to live I''d get back together with my first wife,
because 6 months with her would be like an eternity."
-Bobby Hall

Share this post


Link to post
Share on other sites
With your information, I understand functions and arrays MUCH BETTER. When looking at the code, I could decipher it...almost (you knew there was gonna be a catch ). What is sumSequence's job in this:

#include <stdio.h>
#include <iostream.h>

double square(double doubleVar)
{
return doubleVar * doubleVar;
}
int sumSequence (void)
{
int accumulator=0;
for(;;)
{
double dValue=0;
cout<<"Enter next number: ";
cin>>"dValue";
if (dValue<0)
{
break;
}
int value=(int)square(dValue);
accumulator=accumulator + value;
}
return accumulator;
}
int main(int arg, char* pszArgs[])
{
cout<<"You know what to do!\n";
int accumulatedValue;
do
{
cout<<"\nEnter Next Sequence\n";
accumulatedValue=sumSequence();
cout<<"\nYour total is ";
cout<<accumulatedValue;
} while(accumulatedValue !=0);
int i;
cout<<"Press any key + ENTER to exit\n";
cin>>i;
return 0;
}


BTW, what exactly are arguments? They are used in the ArrayDemo code in my first post: int sumArray (int integerArray[], int sizeOfloatArray). The code in parenthesis are the arguments but what do these do?





[edited by - Stormrage on June 17, 2003 9:22:07 PM]

Share this post


Link to post
Share on other sites
Ok arrays:

Picture an array as a bunch of the same type of variable set down in a row. Arrays are declared like so: typeName arrayName[numVariablesInArray] (= {element_0, element_1, element_2, element_numVariablesInArray-1});

...where the stuff in the parentheses is optional at declaration.

When you use the brackets on an array variable it "returns" the variable that far into the array. This variable can be assigned to and used in expressions just like any other variable. For example:


int myArray[5] = {1,2,3,4,5};
myArray[0] = 5; // First element of the array is assigned the value of 5

int average = (myArray[0]+myArray[1]+myArray[2]+myArray[3]+myArray[4])/5; // returns 3 ... (5+2+3+4+5)/5 = 19/5 = 3 (integer division)



Always make sure you know the length of your arrays because if you try to access or assign to an element beyond the size of the array, you will probably get an access violation error (your program will "crash")


______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!