I know this is simple, but I need a bit of help. I need to write a program that will prompt the user to enter three integers, and should then print the numbers in ascending order. The hint the teacher gave was to use scanf three times? I'm just confused...

Recommended Answers

All 7 Replies

Perhaps you meant to post in the C forum? (This is C++.)

Think about what the program is supposed to do, and write down a sort of "how to" to get it done:

1. get an integer from the user
2. get another integer from the user
3. get yet another integer from the user
4. figure out which integer is the smallest and print it
5. figure out which is neither smallest nor largest and print it
6. figure out which is the largest and print it

Looking that over, you might want to add "ask the user for three integers" before step 1.

Once broken down into smaller steps like this, you can deal with one step at a time. Since you need to keep track of three integers, that suggests three variables: int a, b, c; Use scanf() to get input from the user: scanf( "%d", &a ); // get the first integer from user Refer to your class notes and textbook for more help.

Hope this helps.

Okay, here's what I have so far:

#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();
}

There are six errors so I have a lot of work to do :icon_frown: but is that a start? Or am I completely off?

Perhaps you meant to post in the C forum? (This is C++.)

No, I am using C++.

Think about what the program is supposed to do, and write down a sort of "how to" to get it done:

1. get an integer from the user
2. get another integer from the user
3. get yet another integer from the user
4. figure out which integer is the smallest and print it
5. figure out which is neither smallest nor largest and print it
6. figure out which is the largest and print it

Looking that over, you might want to add "ask the user for three integers" before step 1.

Thanks, that helped.

Okay, here's what I have so far:

#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();
}

There are six errors so I have a lot of work to do :icon_frown: but is that a start? Or am I completely off?

Looks pretty good so far. Here is some work you never knew you had to do:
1) #include <conio.h> -- old, non-portable header. When you get a job you probably can't use it, so stop now.
2) void main () -- main is NEVER a void. It's an int, always has been. See this.
3) clrscr() -- same as #1
4) getch(); -- same as #1
5) Formatting your code. It's not as bad as a lot around here, but check this out for more ideas.

Looks pretty good so far. Here is some work you never knew you had to do:
1) #include <conio.h> -- old, non-portable header. When you get a job you probably can't use it, so stop now.
2) void main () -- main is NEVER a void. It's an int, always has been. See this.
3) clrscr() -- same as #1
4) getch(); -- same as #1
5) Formatting your code. It's not as bad as a lot around here, but check this out for more ideas.

Thanks!

Okay so NOW I have THIS:

#include <iostream>


using namespace std;

int  main ()
{   int x;
    int array [3], t;
    for (x=0; x<3; x++)
    {
    cout << "Enter integer number: " << 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 <<"\n";
       cout <<array[x];
       cout << "\n";
    }
    
    return 0;
}

The only thing is, (sorry if this sounds dumb) when my teacher said that it should print the numbers in ascending order, did he mean that it should actually print out on paper? Because it didn't do that, it just stated it on the screen.

But thanks for the help!

Just on the screen

And your formatting is still not very good.

Just on the screen

And your formatting is still not very good.

Thank you.

It's an intro class that was just a filler.
It works, so it doesn't matter that my formatting isn't good.
I'll still get an A.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.