5 Replies - 49001 Views - Last Post: 12 February 2009 - 07:06 PM Rate Topic: -----

#1 jadedreality  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 08-November 07

Program to print 3 numbers in ascending order

Post icon  Posted 08 November 2007 - 05:04 PM

I have an assignment in which I am supposed to write a code that will prompt the user to enter three integers, and then it should print out the numbers in ascending order. My teacher said that we should use scanf three times, and I'm not sure if that means we HAVE to but I just realized that I didn't do that in my code. Here it is:


#include <iostream>
#include <conio.h>

void main ()
{
clrscr();
int array [3], t;
for (int  x=0; x<3; x++)
{
	cout << "Enter integer number" << x+1 << " : " << endl;
	cin<< array[x];
}
for (x=0; x<3; x++)
{
for (int y=0; y<2; y++)
{
	if(array[y]>array[y+1])
	{
		t=array[y];
		array[y]=array[y+1];
		array[y+1]=t;
	}
}
}
cout << "The integers in ascending order are : ";
for (x=0;x<3;x++)
cout << endl << array[x];
getch();
}



It has six errors :blink: (and two warnings) so I think I have quite a bit more work to do, but I'm kind of stuck.

The errors and warnings are:
C:\ascending.cpp(10) : error C2065: 'clrscr' : undeclared identifier
C:\ascending.cpp(14) : error C2065: 'cout' : undeclared identifier
C:\ascending.cpp(14) : error C2297: '<<' : illegal, right operand has type 'char [21]'
C:\ascending.cpp(14) : error C2065: 'endl' : undeclared identifier
C:\ascending.cpp(15) : error C2065: 'cin' : undeclared identifier
C:\ascending.cpp(15) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
C:\ascending.cpp(29) : error C2297: '<<' : illegal, right operand has type 'char [40]'
C:\ascending.cpp(31) : warning C4552: '<<' : operator has no effect; expected operator with side-effect

This post has been edited by jadedreality: 08 November 2007 - 05:05 PM


Is This A Good Question/Topic? 2

Replies To: Program to print 3 numbers in ascending order

#2 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: Program to print 3 numbers in ascending order

Posted 08 November 2007 - 05:22 PM

I modify your codes with the comments of your wrong coding..

#include <iostream>
#include <conio.h>

using namespace std; // you need this if you eleminate .h in <iostream> headers

int  main ()
{   int x;
	clrscr();
	int array [3], t;
	for (x=0; x<3; x++)
	{
	cout << "Enter integer number: " << endl; //you only print this
	cin >> array[x]; // your syntax here is wrong. this is the correct one
	}
	for (x=0; x<3; x++)
	{
		for (int y=0; y<2; y++)
		{
			if(array[y]>array[y+1])
			{
		t=array[y];
		array[y]=array[y+1];
		array[y+1]=t;
			}
		}
	}
	cout << "The integers in ascending order are : ";
	for (x=0;x<3;x++)
	{  
	   cout <<"\n";
	   cout <<array[x];
	   cout << "\n";
	}
	getch();
	return 0;  // you must always use int main() then return 0 for success. void main has never been used in C++
} 
Hope that helps.

This post has been edited by nirvanarupali: 08 November 2007 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 jadedreality  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 08-November 07

Re: Program to print 3 numbers in ascending order

Posted 08 November 2007 - 05:36 PM

Thank you, that helped a LOT.

When my teacher said that it should print them in ascending order, did he mean it should actually PRINT it out on paper? Because it doesn't, it just says it on the screen.
Was This Post Helpful? 0
  • +
  • -

#7 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: Program to print 3 numbers in ascending order

Posted 08 November 2007 - 05:37 PM

My version using STL container.
#include <iostream>
#include <vector>

using namespace std;

int main()

{
	vector <int> number;
	int x;
	
	for ( int n=0; n<3; n++)
	{
		cout <<"Enter an integer :";
		cin >> x;
		number.push_back(x);
	}
	
	sort (number.begin(),number.end()); // sorting
	
	cout <<"\n Print integers in ascending order :"<< endl;
	
	for ( int i=0; i<number.size(); i++)
	{
		cout <<"\n"<<number.at(i)  << endl;
		
	}
	
	return 0;
	
} 

Was This Post Helpful? 1

#8 samialkindi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-November 07

Re: Program to print 3 numbers in ascending order

Posted 11 November 2007 - 08:09 AM

try this out,, and let me know, see whether it meets ur requirement or not..

#include <conio.h>
#include<iostream>
using namespace std;

void hello();

int main()
{
  hello();
   
  return 0;
  
}
void hello ()
{
//int clrscr();
int array [3], t;
for (int  x=0; x<3; x++)
{
	cout << "Enter integer number" << x+1 << " : " << endl;
	cin>> array[x];
}
for (int i=0; i<3; i++)
{
for (int y=0; y<2; y++)
{
	if(array[i]<array[y])
	{
		t=array[i];
		array[i]=array[y];
		array[y]=t;
	}
}
}
cout << "The integers in ascending order are : ";
for (int x=0;x<3;x++)
cout << endl << array[x];
getch();
}



*(edit jjhaag) - please use code tags :code:

This post has been edited by jjhaag: 11 November 2007 - 01:12 PM

Was This Post Helpful? 0
  • +
  • -

#9 krich87  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 03-December 08

Re: Program to print 3 numbers in ascending order

Posted 12 February 2009 - 07:06 PM

/*
Programmer: Kris Richardson
Date: January 29th, 2009
Description: This is programming excercise 4 for chapter 2
*/
#include <iostream>
#include <conio.h>

using namespace std;

int main ()
{   
	//Declare variables
	int array [3], t;

	//Get the user input and enter it into the array until 3 numbers are reached
	for (int  x=0; x<3; x++)
	{
		cout << "Enter integer number " << x+1 << " : ";
		cin>> array[x];
	}
	for (int i=0; i<3; i++)
	{
		for (int y=0; y<2; y++)
		{
			if(array[i]<array[y])
			{
				t=array[i];
				array[i]=array[y];
				array[y]=t;
			}
		}
	}
	cout << endl;

	//print the three integers in ascending order
	cout << "The integers in ascending order are:";
	for (int x=0; x<3; x++)
	cout << " " << array[x];
	cout << endl;
	cout << endl;

	return 0;
}



Heres my copy, yours helped me along the way though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1