Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 86,392 C++ Programmers. There are 1,398 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

single dimension array integers

 
Reply to this topicStart new topic

single dimension array integers, analyzing and return numeric data

Rahlcepx516
post 8 May, 2008 - 09:41 AM
Post #1


New D.I.C Head

*
Joined: 19 Feb, 2008
Posts: 12



My main goal here is to have these return values. I think the code is for the most part set up for this besides the return code. In which, I am having trouble figuring out what I need to be posting in order for something to return a value, I don't want someone to write code for the solution yet, just hints and ideas as to what I need to do.

Sum – return a sum of all of the data in the array
CountNmbrsBigger – return a count of the number of data elements in the array that are larger than a given integer
Average – returns an average of the data in an array
High – returns the highest value in the array
Low –returns the lowest value in the array
Find(number) – returns the first index of the location of the number in the array


CODE
#include <iostream>

using namespace std;

int sum(int [], int);
int countNmbrsBigger(int [], int, int);
int average(int [], int);
int high(int [], int);
int low(int [], int);
int find(int [], int, int);


int main (void ){
    const int arraySize = 10;
    int theArray[arraySize] = {1, 5, 25, 10, 12, 9, 24, 24, 22, 2};

    cout << "Sum: " << sum(theArray, arraySize) << endl;
    cout << "Count of numbers bigger than 10: " << countNmbrsBigger(theArray, arraySize, 10) << endl;
    cout << "Average: " << average(theArray, arraySize) << endl;
    cout << "High: " << high(theArray, arraySize) << endl;
    cout << "Low: " << low(theArray, arraySize) << endl;
    cout << "Find: " << find(theArray, arraySize, 25) << endl;
    cout << "Find: " << find(theArray, arraySize, 100) << endl;

}


int sum(int theArray [], int theArraySize){
//returns the sum of the values in theArray

}

int countNmbrsBigger(int theArray [], int theArraySize, int Number){
//count the value in the array greater than the parameter

}

int average(int theArray [], int theArraySize){
//average the values in the array

}

int high(int theArray [], int theArraySize){
//find the highest value in the array

}

int low(int theArray [], int theArraySize){
//find the lowest value in the array
     int lowValue = theArray[0];
     for (int i = 0; i < theArraySize; i++){
          if (theArray[i] < lowValue){
              lowValue = theArray[i];
          }
     }
     return lowValue;
}

int find(int theArray [], int theArraySize, int theNumber){
//return the subscript of the supplied argument
//return -1 if not found

}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


polymath
post 8 May, 2008 - 05:57 PM
Post #2


D.I.C Head

Group Icon
Joined: 4 Apr, 2008
Posts: 78

sum: I'd use a while loop that would add the numbers using a control variable, like:
CODE

int x=0;
int y;
while (x<theArraySize)
{
     y = y + theArray[x];
     x++;
}
return (y);

Of course, this is only basic and has several shortfalls, but you get the basic idea.

CountNumbrsBigger: Use a loop similar to sum, like:
CODE

int x=0;
int y=0;
while (x<theArraySize) {
     if (x>Number) y++;
     x++;
}
return (y);

Very similar to sum, you get the idea.

Average: call sum and divide that by theArraySize

High and Low: use another loop:
CODE

int x=0;
int y=0;
while (x<theArraySize) {
     if (y<theArray[x]) y=theArray[x]; //this is for high--flip the lessthan sign for low
     x++;
}
return (y);

i'm sensing a theme here...

find: I have a c++ code snippet called charFinder or something that does the same thing with chars. you could modify that to use ints. That would be the most complete way to do it, though a simple loop could get the job done, albeit much less effectively:
CODE

int x=0;
int position;
while (x<theArraySize) {
     if (theArray[x]==theNumber) theNumber=x+1
     x++
}
return (position);


Hope this helps.



This post has been edited by polymath: 8 May, 2008 - 05:58 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Rahlcepx516
post 9 May, 2008 - 09:38 AM
Post #3


New D.I.C Head

*
Joined: 19 Feb, 2008
Posts: 12

Yep! Much appreciated too!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
Time is now: 5/17/08 05:09AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month