Hello,

I have this function

void smallAverage(int *array, float *average, int *numSmallerThanAverage );

which returns in the parameters, average and numSmallerThanAvergae, the corresponding result. average is the average value of all the elements of the array. numSmallerThanAverage is the number of elements in the array which are smaller than the average.

Now, I need to test the function in a main routine and print the results. The main function must accept the size of the array as a command line parameter and the array should be initialized with random numbers.

Here is what I have so far from code, but I dont know why its doesnt work with me, and to be honest I'm new to C++ and I have no idea how to do a main routine, but I tried my best with the help of google :(

Please any help from anyone with that would be appreciated please!

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <time.h>

using namespace std;

/****************************************function # 1***********************************************/
void smallAverage(int *myArray, float *average, int *numSmallerThanAverage)
{
	int size;
	int sum = 0;
	for(int i = 0; i < size; ++i){
		sum += myArray[i];
    }
    *average = (float)sum/(float)size;
    *numSmallerThanAverage = myArray[0];
	for(int i = 0; i < size; i++)
		cout << myArray[i] << " ";
	for(int i = 1; i < size; i++) //check until condition i=size
	{
		if(*numSmallerThanAverage > myArray[i])
		     *numSmallerThanAverage = myArray[i];
	}
	cout << "\nSearching..." << endl;
	//display the result...
	cout << "The number of elements smaller than the average value is = " << *numSmallerThanAverage << endl;
}	


int main(int argc, char* argv[])
{
	int* myArray = NULL;
                float* average;
	int size;
                cout << "Average: " << *average << '\n';
	myArray = new int[size];
	for(int i = 0; i < size; i++){ 
		myArray[i] = 0;
	}

	delete[] myArray;
	return 0; 
}

Function prototypes must be declared before the main().

void smallAverage(int *myArray, float *average, int *numSmallerThanAverage);
int main()
{
//text of main
return 0;
}

//now begin function

Functions should appear after the main() function.

Edited 7 Years Ago by Grn Xtrm: n/a

I still didnt get an answer to my questions though?

Oh, that's too bad. :(
By the way, "I don't know how to do this" is not a question.
You need to let the community know what errors the compiler is reporting so we can help you more easily and more effectively.

well, I cannot even run it because somehow my main is not proper I guess and I'm new to c++ and I have no idea how to do a main routine and I dont even know how to start it at all, but I somehow searched on the internet and read about it and tried to write the code that I showed earlier :(

Here is how a program containing a function should look:

#include <iostream>
using namespace std;
//here you put the function prototype as I posted earlier
//now begin main()
int main()
{
// here you put the text of the main function--including 
//variable declarations and initialization, function calls, etc.
return 0;
}
//now you write your function

Edited 7 Years Ago by Grn Xtrm: n/a

You don't have to destroy my credibility because you don't like the way I answered your questions. Maybe someone else will read your post and be of more service. Down voting is supposed to be for posts that are unrelated to the topic or disobey the forum rules or are incorrect. Not to get even with someone who will not answer your questions the way you want.

Edited 7 Years Ago by Grn Xtrm: n/a

If you are intending me, I never down graded you...I was actually busy with this program that is giving me a hard time... it must have been another person who voted you down... i dont know.

Edited 7 Years Ago by nanoooosha: n/a

Ok. Whatever. This is not a forum for exchanging verbal abuse, it is a forum designed to help with writing and understanding programming code (and other computer related topics). I'm sorry I could not be of more help and I hope you will come to a quick resolution to your problem.
Good luck.

Edited 7 Years Ago by Grn Xtrm: n/a

I have this function

void smallAverage(int *array, float *average, int *numSmallerThanAverage );

Is that signature a design requirement? Knowing the size of the array is generally done by also passing a size parameter, or else there must be some sort of value used as a sentinel to stop looping. I'd prefer this if given the opportunity:

void smallAverage(int *array, int size, float *average, int *numSmallerThanAverage );

There are many place in your code where you have a 'size' variable, but this is never given a value.

The main function must accept the size of the array as a command line parameter and the array should be initialized with random numbers.

Well, then it seems 'size' needs to be assigned a value from one of the 'argv' strings. It seems you'd need to back up and work on this part.

Hmm, I guess there is a third way (one that I consider a bit ugly) which is to have a global 'size' that is assigned a value in main and also available in your function with the signature you provided.


[edit]In your main function you'll want actual float values to pass by pointer to the function, not simply pointers to nothing. And you'll need to call your function.

int main(int argc, char* argv[])
{
	int* myArray = NULL;
                float* average;
	int size;
                cout << "Average: " << *average << '\n';
	myArray = new int[size];
	for(int i = 0; i < size; i++){
		myArray[i] = 0;
	}

	delete[] myArray;
	return 0;
}

The highlighted text shows you printfing the value pointed to by average, but that pointer doesn't point anywhere. And you're printing this before you've calculated the average, even before you built your array.[/edit]

BTW, a function definition is also a prototype, so preceding main with the actual function is fine.

Edited 7 Years Ago by Dave Sinkula: n/a

See that is what I was confused about the whole time, I dont understand how can I use argv and argc in my main or I dont have to mention them at all inside my main and they are only declared as arguments? plus for your question about whether

Is that signature a design requirement? Knowing the size of the array is generally done by also passing a size parameter, or else there must be some sort of value used as a sentinel to stop looping. I'd prefer this if given the opportunity:

so I mean that we are not allowed to change the signature because its given like that... and honestly I'm lost about that also...because I dont get the idea of like not giving a specific size, but I know its because that you have to allocate the array dynamically...like basically they want the user to give the size and not me giving the size....I really need help somehow because I feel that this is making have a headache since I keep on thinking about it and I dont know what is wrong with it??

See that is what I was confused about the whole time, I dont understand how can I use argv and argc in my main or I dont have to mention them at all inside my main and they are only declared as arguments? plus for your question about whether

The values argc and argv come from the command line. A search of the forum out to bring more information and examples. Some terms might be "command line arguments argc argv".

This article has been dead for over six months. Start a new discussion instead.