| | |
Im new to this site and c++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2003
Posts: 1
Reputation:
Solved Threads: 0
I just started my first c++ class and we recieved our first assignment.
I dont have a compiler at home and live far away from school.
I have 2 questions:
These are the program requirements:
Program 1:
Prompt the user and let them enter three integers. Store them in three variables. Print the numbers in sorted order, from smallest to largest.
Sample run: (user input underlined)
Input integer 1 : 34
Input integer 2 : 200
Input integer 3 : -14
Sorted : -14 <= 34 <= 200
This is the code:
#include <iostream>
int main ( )
{
int one, two, three, first, sec, third;
cout << "Enter 3 integers./n";
cout << "Input Integer 1 : ";
cin >> one;
cout << "/nInput Integer 2 : ";
cin >> two;
cout << "/nInput Integer 3 : ";
cin >> three;
if ( one >= two && one >= three)
one == first;
if ( two >= one && two >= three)
two == first;
if ( three >= one && three >= two)
three = first;
if (one <= two && one >= three)
one == sec;
if (one >= two && one <= three)
one == sec;
if (two <= one && two >= three)
two == sec;
if (two >= one && two <= three)
two == sec;
if (three <= two && three >= one)
three == sec;
if (three >= two && three <= one)
three == sec;
if (one <= two && one <= three)
one = third;
if (two <= one && two <= three)
two = third;
if (three <= one && three <= two)
three = third;
cout << first << ">=" << sec << ">=" << third << ".";
cout << "Have a Nice Day!";
}
Will it work? Any changes(Im sure there is another way without all those ifs.
Program 2 requirments:
Prompt the user to type an integer in the range 0 - 1000. Allow the user to input an integer (you may assume correct type of input. Whenever the integer is not in the specified range, print an error message and make the user re-enter. Once a valid input is received, compute and print out the sum of the digits of the number.
Sample run 1: (user input underlined)
Please input an integer between 0 and 1000: 1001
* Number not in 0-1000 range. Please re-enter: -1
* Number not in 0-1000 range. Please re-enter: 456
Sum of the digits is 15
Code:
#include <iostream>
int main( )
{
int;
cout << "Enter an integer between and including 0-1000./n";
cin >> num;
if (num < 0 || num > 1000)
{
cout << "Number not between 0-1000./n";
cout << "Enter an integer between and including 0-1000./n";
cin >> num;
}
num1 = num / 100
num2 = (num % 100) / 100
num3 = (num % 100) % 10
cout << “The sum of the digits is : “;
cout << num1 + num2 + num3
}
Thanks for any help you can provide, email me at rymade@yahoo.com
I dont have a compiler at home and live far away from school.
I have 2 questions:
These are the program requirements:
Program 1:
Prompt the user and let them enter three integers. Store them in three variables. Print the numbers in sorted order, from smallest to largest.
Sample run: (user input underlined)
Input integer 1 : 34
Input integer 2 : 200
Input integer 3 : -14
Sorted : -14 <= 34 <= 200
This is the code:
#include <iostream>
int main ( )
{
int one, two, three, first, sec, third;
cout << "Enter 3 integers./n";
cout << "Input Integer 1 : ";
cin >> one;
cout << "/nInput Integer 2 : ";
cin >> two;
cout << "/nInput Integer 3 : ";
cin >> three;
if ( one >= two && one >= three)
one == first;
if ( two >= one && two >= three)
two == first;
if ( three >= one && three >= two)
three = first;
if (one <= two && one >= three)
one == sec;
if (one >= two && one <= three)
one == sec;
if (two <= one && two >= three)
two == sec;
if (two >= one && two <= three)
two == sec;
if (three <= two && three >= one)
three == sec;
if (three >= two && three <= one)
three == sec;
if (one <= two && one <= three)
one = third;
if (two <= one && two <= three)
two = third;
if (three <= one && three <= two)
three = third;
cout << first << ">=" << sec << ">=" << third << ".";
cout << "Have a Nice Day!";
}
Will it work? Any changes(Im sure there is another way without all those ifs.
Program 2 requirments:
Prompt the user to type an integer in the range 0 - 1000. Allow the user to input an integer (you may assume correct type of input. Whenever the integer is not in the specified range, print an error message and make the user re-enter. Once a valid input is received, compute and print out the sum of the digits of the number.
Sample run 1: (user input underlined)
Please input an integer between 0 and 1000: 1001
* Number not in 0-1000 range. Please re-enter: -1
* Number not in 0-1000 range. Please re-enter: 456
Sum of the digits is 15
Code:
#include <iostream>
int main( )
{
int;
cout << "Enter an integer between and including 0-1000./n";
cin >> num;
if (num < 0 || num > 1000)
{
cout << "Number not between 0-1000./n";
cout << "Enter an integer between and including 0-1000./n";
cin >> num;
}
num1 = num / 100
num2 = (num % 100) / 100
num3 = (num % 100) % 10
cout << “The sum of the digits is : “;
cout << num1 + num2 + num3
}
Thanks for any help you can provide, email me at rymade@yahoo.com
•
•
Join Date: Sep 2003
Posts: 22
Reputation:
Solved Threads: 0
Your first program is too big.
Something like:
You said that was your first assignment, so I'm not sure you know how to use functions and whatnot, but the program I made should work. Post back any problems.
As for your second:
Now then, I thought of these from my head. I didn't compile em', but post back with any errors you might have, and I can fix em'.
Something like:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> void Swap(int *first,int *second) { *first ^= second; *second ^= first; *first ^= second; } int main(void) { int numbers[4]; for (int temp = 0;temp<3;temp++) scanf("%d",&numbers[temp]); for (temp=0;temp<2;temp++) { if (numbers[temp] > numbers[temp+1] ) Swap(&numbers[temp],&numbers[temp+1]); } if (numbers[0] > numbers[1]) Swap(&numbers[0],&numbers[1]; return 0; }
You said that was your first assignment, so I'm not sure you know how to use functions and whatnot, but the program I made should work. Post back any problems.
As for your second:
C++ Syntax (Toggle Plain Text)
#include <stdio.h> int main(void) { int temp = -1; while (temp < 0 || temp > 1) { printf("Input number:\n"); scanf("%d",&temp); } for (temp2 = 0;temp;temp/=10) temp2+=temp%10; printf("%d",temp2); return 0; }
Now then, I thought of these from my head. I didn't compile em', but post back with any errors you might have, and I can fix em'.
![]() |
Similar Threads
- News Story: Google Site Search Gets On-Demand Indexing (Search Engine Optimization)
- Streeeeetched the site (DaniWeb Community Feedback)
- site promotion (DaniWeb Community Feedback)
- Review this site!! (Geeks' Lounge)
- This site going very very slow (DaniWeb Community Feedback)
Other Threads in the C++ Forum
- Previous Thread: compilers for c++
- Next Thread: c++ compiling problem
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





