Thread: Another one..

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    Another one..

    I need source code for a program that inputs an int from a user, then I need to allocate an array of doubles on the free store.

    I then need to iterate across the array and display all of the values, then display the highest and lowest values, the total of the values, and the average of the values. I also have to release the free store when the prorgam is done...HELP!!!!

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    This is fairly straight forward, but nobody here is going to write it for you.

    Look up the 'new' and 'delete' operators and if you already haven't, learn about input and output streams and loops. Then have a go at the problem yourself and post your code (in code tags) when you are stuck with a clear question explaining what you are struggling with.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    started...

    OK - i've been working on this for a few minutes, I want this to keep looping until the user has entered 10 items....

    Then i want to start displaying the values i need to:

    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
        int number=10;
        cout << "Enter an integer: " << flush;
        while(!(cin >> number)){
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            cout << "Invalid input, please try again: " << flush;
        }
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        cout << "You entered: " << number << endl;
        
        cin.get();
    }

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here it is:
    Code:
    #if     _MSC_VER > 1000
    #pragma once
    #endif
    
    #ifndef _OSTREAM_
    #define _OSTREAM_
    #include <ios>
    
    #ifdef  _MSC_VER
    #pragma pack(push,8)
    #endif  /* _MSC_VER */
    		// I/O exception macros
     #define _TRY_IO_BEGIN	_TRY_BEGIN
     #define _CATCH_IO_END	_CATCH_ALL \
    	setstate(ios_base::badbit, true); _CATCH_END
     #define _CATCH_IO_(x)	_CATCH_ALL \
    	(x).setstate(ios_base::badbit, true); _CATCH_END
    _STD_BEGIN
    		// TEMPLATE CLASS basic_ostream
    template<class _E, class _Tr = char_traits<_E> >
    	class basic_ostream : virtual public basic_ios<_E, _Tr> {
    public:
    	typedef basic_ostream<_E, _Tr> _Myt;
    	typedef basic_ios<_E, _Tr> _Myios;
    	typedef basic_streambuf<_E, _Tr> _Mysb;
    	typedef ostreambuf_iterator<_E, _Tr> _Iter;
    	typedef num_put<_E, _Iter> _Nput;
    	explicit basic_ostream(basic_streambuf<_E, _Tr> *_S,
    		bool _Isstd = false, bool _Doinit = true)
    		{if (_Doinit)
    		    init(_S, _Isstd); }
    	basic_ostream(_Uninitialized)
    		{_Addstd(); }
    	virtual ~basic_ostream()
    		{}
    	class sentry {
    	public:
    		explicit sentry(_Myt& _Os)
    			: _Ok(_Os.opfx()), _Ostr(_Os) {}
    		~sentry()
    			{if (!uncaught_exception())
    				_Ostr.osfx(); }
    		operator bool() const
    			{return (_Ok); }
    	private:
    		bool _Ok;
    		_Myt& _Ostr;
    		};
    	bool opfx()
    		{if (good() && tie() != 0)
    			tie()->flush();
    		return (good()); }
    	void osfx()
    		{if (flags() & unitbuf)
    			flush(); }
    	_Myt& operator<<(_Myt& (__cdecl *_F)(_Myt&))
    		{return ((*_F)(*this)); }
    	_Myt& operator<<(_Myios& (__cdecl *_F)(_Myios&))
    		{(*_F)(*(_Myios *)this);
    		return (*this); }
    	_Myt& operator<<(ios_base& (__cdecl *_F)(ios_base&))
    		{(*_F)(*(ios_base *)this);
    		return (*this); }
    	_Myt& operator<<(_Bool _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(short _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			fmtflags _Bfl = flags() & basefield;
    			long _Y = (_Bfl == oct || _Bfl == hex)
    				? (long)(unsigned short)_X : (long)_X;
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _Y).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(unsigned short _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), (unsigned long)_X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(int _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			fmtflags _Bfl = flags() & basefield;
    			long _Y = (_Bfl == oct || _Bfl == hex)
    				? (long)(unsigned int)_X : (long)_X;
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _Y).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(unsigned int _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), (unsigned long)_X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(long _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(unsigned long _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(float _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), (double)_X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(double _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(long double _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(const void *_X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (_Ok)
    			{const _Nput& _Fac = _USE(getloc(), _Nput);
    			_TRY_IO_BEGIN
    			if (_Fac.put(_Iter(rdbuf()), *this,
    				fill(), _X).failed())
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& operator<<(_Mysb *_Pb)
    		{iostate _St = goodbit;
    		bool _Copied = false;
    		const sentry _Ok(*this);
    		if (_Ok && _Pb != 0)
    			for (int_type _C = _Tr::eof(); ; _Copied = true)
    				{_TRY_BEGIN
    				_C = _Tr::eq_int_type(_Tr::eof(), _C)
    					? _Pb->sgetc() : _Pb->snextc();
    				_CATCH_ALL
    					setstate(failbit);
    					_RERAISE;
    				_CATCH_END
    				if (_Tr::eq_int_type(_Tr::eof(),_C))
    					break;
    				_TRY_IO_BEGIN
    					if (_Tr::eq_int_type(_Tr::eof(),
    						rdbuf()->sputc(_Tr::to_char_type(_C))))
    						{_St |= badbit;
    						break; }
    				_CATCH_IO_END }
    		width(0);
    		setstate(!_Copied ? _St | failbit : _St);
    		return (*this); }
    	_Myt& put(_E _X)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (!_Ok)
    			_St |= badbit;
    		else
    			{_TRY_IO_BEGIN
    			 if (_Tr::eq_int_type(_Tr::eof(),
    				rdbuf()->sputc(_X)))
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& write(const _E *_S, streamsize _N)
    		{iostate _St = goodbit;
    		const sentry _Ok(*this);
    		if (!_Ok)
    			_St |= badbit;
    		else
    			{_TRY_IO_BEGIN
    			if (rdbuf()->sputn(_S, _N) != _N)
    				_St |= badbit;
    			_CATCH_IO_END }
    		setstate(_St);
    		return (*this); }
    	_Myt& flush()
    		{iostate _St = goodbit;
    		if (!fail() && rdbuf()->pubsync() == -1)
    			_St |= badbit;
    		setstate(_St);
    		return (*this); }
    	_Myt& seekp(pos_type _P)
    		{if (!fail())
    			rdbuf()->pubseekpos(_P, out);
    		return (*this); }
    	_Myt& seekp(off_type _O, ios_base::seekdir _W)
    		{if (!fail())
    			rdbuf()->pubseekoff(_O, _W, out);
    		return (*this); }
    	pos_type tellp()
    		{if (!fail())
    			return (rdbuf()->pubseekoff(0, cur, out));
    		else
    			return (streampos(_BADOFF)); }
    	};
    
    
    #ifdef _DLL
    #pragma warning(disable:4231) /* the extern before template is a non-standard extension */
    extern template class _CRTIMP basic_ostream<char, char_traits<char> >;
    extern template class _CRTIMP basic_ostream<wchar_t, char_traits<wchar_t> >;
    #pragma warning(default:4231) /* restore previous warning */
    #endif		// _DLL
    
    
    		// INSERTERS
    template<class _E, class _Tr> inline
    	basic_ostream<_E, _Tr>& __cdecl operator<<(
    		basic_ostream<_E, _Tr>& _O, const _E *_X)
    	{typedef basic_ostream<_E, _Tr> _Myos;
    	ios_base::iostate _St = ios_base::goodbit;
    	size_t _N = _Tr::length(_X);
    	size_t _M = _O.width() <= 0 || _O.width() <= _N
    		? 0 : _O.width() - _N;
    	const _Myos::sentry _Ok(_O);
    	if (!_Ok)
    		_St |= ios_base::badbit;
    	else
    		{_TRY_IO_BEGIN
    		if ((_O.flags() & ios_base::adjustfield)
    			!= ios_base::left)
    			for (; 0 < _M; --_M)
    				if (_Tr::eq_int_type(_Tr::eof(),
    					_O.rdbuf()->sputc(_O.fill())))
    					{_St |= ios_base::badbit;
    					break; }
    		if (_St == ios_base::goodbit
    			&& _O.rdbuf()->sputn(_X, _N) != _N)
    			_St |= ios_base::badbit;
    		if (_St == ios_base::goodbit)
    			for (; 0 < _M; --_M)
    				if (_Tr::eq_int_type(_Tr::eof(),
    					_O.rdbuf()->sputc(_O.fill())))
    					{_St |= ios_base::badbit;
    					break; }
    		_O.width(0);
    		_CATCH_IO_(_O) }
    	_O.setstate(_St);
    	return (_O); }
    template<class _E, class _Tr> inline
    	basic_ostream<_E, _Tr>& __cdecl operator<<(
    		basic_ostream<_E, _Tr>& _O, _E _C)
    	{typedef basic_ostream<_E, _Tr> _Myos;
    	ios_base::iostate _St = ios_base::goodbit;
    	const _Myos::sentry _Ok(_O);
    	if (_Ok)
    		{size_t _M = _O.width() <= 1 ? 0 : _O.width() - 1;
    		_TRY_IO_BEGIN
    		if ((_O.flags() & ios_base::adjustfield)
    			!= ios_base::left)
    			for (; _St == ios_base::goodbit && 0 < _M; --_M)
    				if (_Tr::eq_int_type(_Tr::eof(),
    					_O.rdbuf()->sputc(_O.fill())))
    					_St |= ios_base::badbit;
    		if (_St == ios_base::goodbit
    			&& _Tr::eq_int_type(_Tr::eof(),
    				_O.rdbuf()->sputc(_C)))
    			_St |= ios_base::badbit;
    		for (; _St == ios_base::goodbit && 0 < _M; --_M)
    			if (_Tr::eq_int_type(_Tr::eof(),
    				_O.rdbuf()->sputc(_O.fill())))
    				_St |= ios_base::badbit;
    		_CATCH_IO_(_O) }
    	_O.width(0);
    	_O.setstate(_St);
    	return (_O); }
    template<class _E, class _Tr> inline
    	basic_ostream<_E, _Tr>& __cdecl operator<<(
    		basic_ostream<_E, _Tr>& _O, const signed char *_X)
    	{return (_O << (const char *)_X); }
    template<class _E, class _Tr> inline
    	basic_ostream<_E, _Tr>& __cdecl operator<<(
    		basic_ostream<_E, _Tr>& _O, const signed char _C)
    	{return (_O << (char)_C); }
    template<class _E, class _Tr> inline
    	basic_ostream<_E, _Tr>& __cdecl operator<<(
    		basic_ostream<_E, _Tr>& _O, const unsigned char *_X)
    	{return (_O << (const char *)_X); }

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    tha'ts a lot of code for something that seems so simple....do you have anything else?

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    I do appreciate your help man, believe me!

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    syntax

    Thanks - i'm horrible at syntax help!!

    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
        int number;
    	int a[10];
    	int i;
    for ( i = 0 ; i < 10 ; i++ ) cin >> a[i];
    	cout << "Enter an integer: " << flush;
        while(!(cin >> number)){
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            cout << "Invalid input, please try again: " << flush;
        }
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        cout << "You entered: " << number << endl;
        
        cin.get();
    }

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    OK - I have it somewhat now...

    When the program prints, I wanted it to print all of the items of the array, it prints -858993460 instead of the numbers I've typed in...any ideas?

    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int main()
    {
        int number;
    	int a[10];
    	int i;
    
    	cout << "Enter an integer: " << flush;
    	for ( i = 0 ; i < 10 ; i++ ) cin >> a[i];
    	{
    	while(!(cin >> number)){
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            cout << "Invalid input, please try again: " << flush;
    		}	
    	}
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        cout << "You entered: " << a[i] << endl;
        cin.get();
    }

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Originally posted by jpipitone
    tha'ts a lot of code for something that seems so simple....do you have anything else?
    Yeah, that was the point. S/he gave you something that you wouldn't be able to pass in.

    BTW, 7Stud, please tell me that you didn't waste your time writing that.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    OK - The highest value is being returned -1.....how come?

    I'm stuck

    Code:
    #include <iostream.h>
    
    //using namespace std;
    
    int main()
    {
    int number;
    double highestValue = -1;
    double a[9];
    int i=0;
    
    cout << "Enter an integer: ";
    {	
    if ( a[i] > highestValue )
    	highestValue = a[i];
    }
    {
    	for ( i = 0 ; i < 9 ; i++ )
    	cin >> a[i];
    	
    }
    
    cout << "You entered: " << endl;
    {
    	for ( i = 0 ; i < 9 ; i++ )
    	cout << "\t" << a[i] << endl;
    
    }
    
    cout << "\nThe highest value is: " << highestValue << endl;
    
    }

  11. #11
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Because you compare highestValue with an uninitialized value.
    Code:
    /* Always initialize an object or array before you use it*/
    double a[9];
    ......
    if ( a[ i ] > highestValue )
    Your logic for the program should be something like this
    • Fill array with values
    • Print them (if you want it of course)
    • Set highestValue to the first element in the array
    • loop through the array(start at the second element) and check for highest value
    • Print the highest value


    Give it another try and if something is still unclear just ask .
    Last edited by ripper079; 05-18-2003 at 04:03 PM.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things don´t come easy in life!!!

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    OK - I got the print the largest value working, now I'm confused as to how to print the smaller value....

    help?

    Code:
    #include <iostream.h> 
    void prompt();        
    void print();         
    double ar[10]={0};     
    int index;            
    float max = 0; 
    float min = 1;
    int i=0;
    
    void prompt()         
    {  
    	double value;    
    	for(int count=0; count<10; count++) 
    		{          
    			cout<<"\nPlease enter an integer:";
    			cin>>value;        	
    			if(value>max)  
    			{      
    					max = value;     
    					index = count;   
    			}       
    			ar[count] = value; 
    		}             
    }   
    void print()  
    {
    		cout << "You entered: " << endl;
    	{
    		for ( i = 0 ; i < 9 ; i++ )
    		cout << "" << ar[i] << endl;
    	}
    
    	cout<<"\nThe maximum value entered was: "<<max<<endl; 
    }		
    
    void main()  
    {
    	prompt();  
    	print();   
    }

  13. #13
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Actually your program has a bug , what happend if the user inputs all negative numbers??? You will get a max number which is 0 . Beacuse I have to go now I will give you the whole source code. I just follow the steps I described for you in my previous answer

    Code:
    /* Use new headers insteed of old */
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    /* main return ALWAYS an int in c++ */
    int main()
    {
    /* Useless
       int number;
    */
       double highestValue = 0;
       double lowestValue = 0;
       double a[9];
       int i=0;
       cout << "Enter an integer: ";
       {
    /* Filling the array */
          for ( i = 0 ; i < 9 ; i++ )
                cin >> a[i];	
       }
    
    cout << "You entered: " << endl;
        {
    /* Printing the array */
    	    for ( i = 0 ; i < 9 ; i++ )
    	         cout << "\t" << a[i] << endl;
        }
    /* Set highest and lowest value to first element in array */
        highestValue = a[0];
        lowestValue = a[0];
        for (i = 1; i < 9; ++i)
        {
    /*Compare if vector has a higer/lower value then  highestValue/lowestValue  */
            if (a[i] > highestValue)
                    highestValue = a[i];
            if (a[i] < lowestValue)
                    lowestValue = a[i];       
        }
    /* Print information */
    cout << "\nThe highest value is: " << highestValue << endl;
    cout << "\nThe lowest value is: " << lowestValue << endl;
    getch();
    /* Return 0 */
    return 0;
    }
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things don´t come easy in life!!!

Popular pages Recent additions subscribe to a feed