# calculating the average, smallest and biggest of the numbers given to

Page 1 of 1

## 9 Replies - 1554 Views - Last Post: 13 December 2009 - 02:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=145104&amp;s=59771b0877d696db411d975df6292ddb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 toofanuk

Reputation: 0
• Posts: 7
• Joined: 12-December 09

# calculating the average, smallest and biggest of the numbers given to

Posted 12 December 2009 - 12:34 PM

hi, this is my first post here, i hope i can make the most of the community.

basically im trying to write a c++ programme.i know argv and argc has to be involved but here is what i have so far:

```#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
int largest, smallest ;
double average ;

for (int i = 0 ; i < argc ; i++)
{
cout<<argv[i] << " " ;
}
cout<<endl;

}
```

but im stuck, im expected to get this:

> ./a.out 21 1 8 15 10 8
Largest = 21
Smallest = 1
Average = 6.5

any ideas?

This post has been edited by JackOfAllTrades: 12 December 2009 - 12:36 PM

Is This A Good Question/Topic? 0

## Replies To: calculating the average, smallest and biggest of the numbers given to

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 12 December 2009 - 12:38 PM

Where are you doing anything with the command line parameters other than printing them? I don't see any calculations being done at all.

### #3 toofanuk

Reputation: 0
• Posts: 7
• Joined: 12-December 09

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 12 December 2009 - 12:42 PM

i know, this is where im stuck

right now im just printing out the numbers given to the array.

but i want to know is the a function which can see the biggest number
entered by the user and detect it.

### #4 olibenu

Reputation: 45
• Posts: 538
• Joined: 31-December 08

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 12 December 2009 - 12:46 PM

try bubble sorting (look it up if u don't know what it means) the elements of the argv array

### #5 toofanuk

Reputation: 0
• Posts: 7
• Joined: 12-December 09

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 12 December 2009 - 12:55 PM

thanks guys.

i think im going to try using a temp location to compare the numbers one by one in a if statement.

but i have a feeling i will come heading to problems.

### #6 toofanuk

Reputation: 0
• Posts: 7
• Joined: 12-December 09

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 12 December 2009 - 01:22 PM

first im trying to do the biggest number but im having problems:

```
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
int largest, temp, smallest ;
double average ;

for (int i = 0 ; i < argc ; i++)
{
temp = argv[i];

if(argv[i] <= temp)
{

i++;

}
else
{
argv[i] = temp;
}
cout <<"largest number is: " << temp ;

}
}
```

common guys, any tips and hint?

This post has been edited by JackOfAllTrades: 13 December 2009 - 03:30 PM

### #7 toofanuk

Reputation: 0
• Posts: 7
• Joined: 12-December 09

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 12 December 2009 - 01:56 PM

and still no help...

### #8 fushar

Reputation: 2
• Posts: 32
• Joined: 12-December 09

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 12 December 2009 - 04:13 PM

First there are few things to note.
1. argc also counts the name of the program, so argv[0] is the name of the program, and argv[1]..argv[argc-1] are the arguments to the program.
2. argv[] is of type char*, not int. So you have to cast it properly to int.

Here is my code.

```#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <limits>
using namespace std;

int main(int argc, char* argv[])
{
// initializes the values of largest, smallest, and average
int largest = numeric_limits<int>::min();
int smallest = numeric_limits<int>::max();
double average = 0;

for (int i = 1; i < argc; i++)
{
int cur;

// the char*-to-int conversion
string s(argv[i]);
istringstream ss(s);
ss >> cur;

// update
largest = max(largest, cur);
smallest = min(smallest, cur);
average += cur;
}
average /= (argc-1);

cout << "Largest = " << largest << endl;
cout << "Smallest = " << smallest << endl;
cout << "Average = " << average << endl;
}

```

### #9 toofanuk

Reputation: 0
• Posts: 7
• Joined: 12-December 09

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 13 December 2009 - 02:04 PM

many thanks.
can i use the atoi() function isntead, to convert string to int?

This post has been edited by toofanuk: 13 December 2009 - 02:11 PM

### #10 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: calculating the average, smallest and biggest of the numbers given to

Posted 13 December 2009 - 02:27 PM

toofanuk, on 13 Dec, 2009 - 01:04 PM, said:

can i use the atoi() function isntead, to convert string to int?

I don't know.
Can you write that code?
Why not give a try and show us some code.
And when you post it wrap it in code tags