9 Replies - 916 Views - Last Post: 13 December 2009 - 02:27 PM Rate Topic: -----

#1 toofanuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?

*** MOD EDIT: Added code tags. Please :code: ***

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

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 toofanuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 537
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 toofanuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 toofanuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?

*** MOD EDIT: Added code tags. Please :code: ***

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

Was This Post Helpful? 0
  • +
  • -

#7 toofanuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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...
Was This Post Helpful? 0
  • +
  • -

#8 fushar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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;
}


Was This Post Helpful? 1
  • +
  • -

#9 toofanuk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • 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

View Posttoofanuk, 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
:code:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1