Solutions requirements Solutions must: Use only standard C++. Be compilable. Be in accordance to general coding practices. (no esoteric demonstrations are required) and should: Handle error situations, even if behavior is not defined. Acceptable default is simply reporting an "Error". Be internally consistent in relation to the adopted coding-style. Be as simple as possible on to the point (use the minimum required variables and function calls/classes and reduces convoluted I/O handling). Please do not add solutions that are 99% similar to another that is already present, if it is an improvement just add it to the existing solution. Write a program that asks the user to type an integer and writes "YOU WIN" if the value is between 56 and 78 (both included). In the other case it writes "YOU LOSE". Solution //Example 1 #include using namespace std; int main() { int input; bool win = false; do { cout << "Please enter a number: " << flush; cin >> input; if (56 <= input && input >= 78) { cout<<"You win! "< using namespace std; int main () { int a; cout<<"your Age"<>a; if(a>=58 && a<=73) { cout<<"You win"< using namespace std; int main() { cout << "\t\t\t Kheang's Random Number Lotto\n\n"; cout << "Please pick a number between 0 and 100\n"; int num; cin >> num; if ((num <= 78) && (num >= 56)){ cout << "Congratulations! YOU WIN! ";} else { cout << "Unfortunately YOU LOSE! "; } } //Example 4 #include using namespace std; int main() { int num; cout<<" enter a number between 56 and 78\n'; (num >= 56 && num <= 78)? cout<<"you win" : cout<<"you lost"; } //Example 5 #include using namespace std; int main() { int num; cout<<"Enter any number"<>num; if(num>=56&#<=78) cout<<"YOU WIN"< #include int n; main() { cout<<"Enter an integer: ";cin>>n; if(n>55&&n<79){cout<<"YOU WIN! ";} else{cout<<"YOU LOSE! ";} getche(); return 0; } //Example 6 #include using namespace std; int main() { int i; cout << "Type a number between 56 and 78: " << endl; cin >> i; if (i>=56 && i<=78) { cout << "YOU WIN" << endl; } else { cout << "YOU LOSE" << endl; } return 0; } //Example 7 #include int main() { int num; std::cout << "Enter a value between 56 and 78\n"; std::cout << "Enter a number: "; std::cin >> num; if(num >= 56 && num <= 78) { std::cout << "You Win!\n"; } else { std::cout << "You Lose!\n"; } return 0; } //Example 8 #include // std::cout, std::cin using namespace std; int main() { // The user input int input; // Ask for a number cout << "Please, enter a number: "; // Get the user input cin >> input; // While the user is not inputting a correct number... while (cin.fail()) { // Ask again for input cout << "Please, enter a valid number: "; // Clear the input we got before cin.clear(); // Ignore the new line character cin.ignore(256, '\n'); // Get the input again cin >> input; } // If the input is [56, 78], YOU WIN if (input >= 56 && input <= 78) cout << endl << "YOU WIN" << endl; else // Else YOU LOSE cout << endl << "YOU LOSE" << endl; return 0; } //Example 9 #include #include #include int main() { int val; do { printf("Write a value: "); scanf("%i",&val); if (val <= 78 && val >=56) { printf("YOU WIN!\n"); getch(); } else { printf("YOU LOSE! "); getch(); system("cls"); } } while(val > 78 || val < 56 ); } }} //Example 10 #include void main() { int nInteger; std::cout << "Enter an integer: "; std::cin >> nInteger; (nInteger > 56 && nInteger < 78 ? std::cout << "YOU WIN" << std::endl : std::cout << "YOU LOSE" << std::endl); system("Pause"); } //Example 11 #include #include #include using namespace std; #define newline cout<<'\n' void check(void); int main() { check();//everything takes place inside check() function. return 0; } void check(void) { string user_input; int input; printf("Enter an integer: "); getline(cin, user_input); stringstream(user_input) >> input; if (input >= 56 && input <= 78) { //checks user_input with boolean parameters. printf("YOU WIN! "); } else printf("YOU LOSE! "); newline; } //Example 12 #include int main() { std::cout << "Enter an integer: __\b\b"; int ans; std::cin >> ans; std::cout << std::endl; std::cout << ( 56 < ans and ans < 78 ? "Win!" : "Loose!" ); return 0; } //Example 13 #include int main() { int a = 0; std::cout<<"Type in integer: "; std::cin>>a; std::cout<= 56 && a <= 78) { std::cout<<"YOU WIN! :>"; } else { std::cout<<"YOU LOSE :<"; } return 0; } //Example 14 #include int inpt; using namespace std; int main(int argc, char** argv) { cin>>inpt; if(inpt>55&&inpt<79)cout<<"You win! "; else cout<<"You Lose"; return 0; } Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. Solution //Example 1 //considering the user already learned Arrays #include using namespace std; int main() { int input, arr[16], i = 0; bool duplicate = false; cout << "Please enter integers between 8 and 23: " << flush; for(;;) { cin >> input; if (input >= 8 && input <= 23) { for (int j = 0; j <= i; j++) { if (input == arr[j]) { duplicate = true; } } if (duplicate) { cout << "Sorry the integer is already entered, try another one: " << flush; duplicate = false; } else { arr[i] = input; cout << "Correct, please proceed to the next integer: " << flush; i = i + 1; } } else { cout << "Incorrect, please enter integers between 8 and 23: " << flush; } } return 0; } //by Serenity: //Simpler way then 999 code lines... int main() { int t_int; for (int i = 8; i < 23; i++) { std::cout << "Type numbers 8 util 23" << std::endl; std::cin >> t_int; if (t_int < i) { std::cout << "Invalid number" << std::endl; } else if (t_int > i && t_int > 23) { std::cout << "Invalid number" << std::endl; } else { std::cout << "Valid number" << std::endl; } } system("PAUSE"); } // Written by S.SNGWN #include #include void main(){ int n,i; cout<<"Type all integers between 8 and 23 both included \n"; for(i=8;i<=23;i++) cin>>n; getch(); } //Made by Lam P. #include using namespace std; int main() { int nu; for(int n=8; n<=23;n++) { cin>>nu; if(nu==n) { cout<<"Correct! Next number is: "; } else { cout<<"Wrong! Try again: "; n=n-1; } } } //Made by: Engineer Reiz Troy D. Durante #include #include int i,j,k,p,n[16]; main(){ clrscr(); for(i=8;i<25;i++){n[i-8]=i;} for(i=0;k<16;i++){ cout<<"The program will end once all required numbers were all given.\nEnter numbers between 8 and 23 (both inclusive): "; cin>>p; for(j=8;j<25;j++){ if(n[j-8]==p){k++;n[j-8]=0;} } clrscr(); } getche(); return 0; } /******************************************* * Made by Pedro Casagrande, UFRGS - BRAZIL * *******************************************/ #include using namespace std; int Range(int);//just declaring the function int main () { int num[16],i, j; cout << "type all integers numbers between 8 and 23 [inclusive]:\n"; for (i = 0; i < 16; i++) { cout << i+1 <<"o number: "; cin >> num[i]; //seeing if the number is out of range, if it is... u need to change it num[i] = Range(num[i]); //cout << num[i] << "\n"; //if u want to see the new number for(j=0; j != i; j++) //seeing if the number have been already typed { while(num[i]==num[j]) // if is the same, u need to change, until it is not the same as another aready typed { cout << "u have already typed this number. Type again:\n"; cin >> num[i]; //seeing if the number is out of range, AGAIN num[i] = Range(num[i]); j=-1; } } cout << "Nice!\n"; // just to show u succeeded with the new number } cout << "\nYOU HAVE DONE IT!\n"; return 0; } int Range(int a) { while(! (a>=8 && a<=23)) //test if the number is out of range { cout << "the number u'v typed are out of range. Type again:\n"; cin >> a; } return a; } #include #include using namespace std; /*************************************************************************************/ /*Soluiton submited by Joshua Waiswa, Makerere University Kampala, jwaiswa7@gmail.com*/ /*************************************************************************************/ int main(){ int x,j,t; int z[16]; cout <<"Enter numbers between 8 and 23, 8 and 23 inclusive"<> j; /*If statement checks if the number entered is between 8 and 23*/ if(j>=8 && j<=23){ /*Value of t places the entered number in its position within the array */ t = j-8; z[t] = j; }else{ /*When an entry is wrong, the counter sets back one step */ cout <<"Wrong entry"< int main() { int num; std::cout << "Type all the integers between 8 and 23\n"; for(int cont=8; cont <= 23; cont++) { std::cout << "Enter a number: "; std::cin >> num; if(num != cont) { std::cout << "Error!\n"; cont--; } } return 0; } //alternative solution by Afrim Kamberi #include using namespace std; int main() { int n = 8; cout << "current number is 8" << endl; for (int i = 8; i < 23; i++) { cout << "put the following number"; cin >> i; n += 1; if (i != n) { cout << "wrong entry,try again" << endl; i -= 1; n -= 1; } } return 0; } //Solution #include using namespace std; int main() { // Input and loop defined, only adds if input is correct. // EDIT: Incrementing the LCV by a truth value is poor form. for (short int i = 8, input; i <= 23; i += (i == input)) { cout << "Enter the number " << i << ": "; cin >> input; } return 0; } //Kyung Shin //16/09/2014 #include using namespace std; int main(){ short int input,inc=0; for(short int i=8;i<=23;i+=inc){ cout<<"Enter number "<>input; if(input==i) inc=1; else inc=0; } return 0; } //Suneet Kumar Saini //26/08/2014 #include using namespace std; int main() { int num, size, i=0; int arr[16]; cout<<"print the number between 8 to 23 "<>num; if(num >= 8 & num <= 23) { if(i == 0) { arr[i] = num; i++; } else { for(int j=0; j<=i; j++) { if(arr[j] != num) { arr[i] = num; i++; } else { cout<<"Entered number PREVIOUSLY EXISTED"< using namespace std; int main() { int num; for (int i = 8; i <= 23;) { cout <<"Enter number "<< i <<": "; cin >> num; if (num == i) { i++; } else { cout <<"You've entered invalid number, Try again. "< #define MIN 8 #define MAX 23 using namespace std; bool isInHistory(int arr[], int n) { for(int i=0; i "; cin >> input; if(input>=MIN && input<=MAX && !isInHistory(history, input)){ history[i]=input; cout << "ok.\n"; } else { cout << "not ok.\n"; i--; } } return 0; } //alternate solution by Gunnar Godara #include using namespace std; void main() { int i,num; //enter value in for loop for(i=8;i<=23;i++) { cout<<"enter the number = "; cin>>num; if(num==i) { cout< using namespace std; int main() { int num; cout << "Type all numbers from 8 to 23\n"; cout << "start now: "; for (int i = 8; i <= 23;) { cin >> num; cout << "next: "; if (num != i) { cout << "Invalid number, next is: " << i << endl; cout << "next: "; } else i++; } cout << "Congratulations! !\n"; return 0; } #include //Write a program that asks the user to type all the integers between 8 and 23 //(both included) using a for loop. int main() { int a, b; cout << "\nEnter numbers from 8-23"; cin >> b; if ((b < 8) || (b > 23)) cout << "invalid"; else { for (a = 8; a < 23; a++) { cin >> b; if (b > 23) cout << "\nExceeds required input"; } } return 0; } #include using namespace std; int main() { int typedInt = 0; cout << "Type all numbers between 8 and 23: " << endl; for (int i = 8; i <= 23; i++) { cin >> typedInt; if (typedInt != i) { cout << "You Missed the sequence the next number was " << i << endl; --i; } } } /*EXERCISE 2 ~asdfk Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. */ #include using namespace std; int main() { int num, next(8); for (; next < 25;) { cout << "Please enter a number: "; cin >> num; if (num == next) { next++; } else { cout << "Wrong input, actual number: " << next << endl; return 0; } } cout << "Congratulations\n"; return 0; } //To the point answer, provided by the Persian Sphinx //Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. #include using namespace std; int main() { int x, i; for (i = 8; i <= 23; i++){ cout << "Type in the integer '"; cout << i; cout << "'" << endl; cin >> x; } return 0; } Alternate solution #include using std::endl; using std::cout; using std::cin; int main() { int i=0; int askNum =0; for (int i = 8; i <= 23; i++) { cout << endl; cout << "Type the any Integer between 8 to 23 to proceed ; Current Iteration :" << i << endl; cin >> askNum; if (8 <= askNum && 23 >= askNum) cout << "Typed Number : " << askNum << endl; else cout << "You typed a wrong number repeat again" << endl; } return 0; } Solution in C #include #define MIN_VALUE 8 #define MAX_VALUE 23 int main(int argc, char *argv[]) { int i; int value; printf("Please input all the numbers from %d to %d (%d and %d included)\n", MIN_VALUE, MAX_VALUE, MIN_VALUE, MAX_VALUE); for (i = MIN_VALUE; (i >= MIN_VALUE) && (i <= MAX_VALUE); i++) { scanf("%d", &value); if (value != i) { printf("The number was %d... You failed!\n", i); } } printf("Congratulations!\n"); return 0; } Another alternate solution /* * http://en.wikibooks.org/wiki/C++_Programming/Exercises/Iterations * * Exercise 2: * Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. */ //Provided by someone who got bored on May 8, 2012 #include using namespace std; //This version is a little more complicated, but it allows the numbers to be input in any order int main() { short int numbers[16]; //array of short ints with capacity 16; ((23 - 8) + 1 for including 8) bool valid; for (short int i = 8, input; i <= 23; i+=(valid)){ //borrowed from a previous solution valid = false; cout << "Enter a value between 8 and 23 (inclusive): "; cin >> input; if (input >= 8 && input <= 23) //compare input with range { if (numbers[input-8] == input) //8 must be subtracted from input in order to get correct index { cout << "You have already entered that value.\n"; } else { numbers[input-8] = input; //this will essentially sort the array, allowing efficient access and comparison valid = true; //valid value was entered } } else { cout << "That value is not between 8 and 23!\n"; } } cout << "Done. "; return 0; } //alternate solution by James Neill #include using namespace std; //does not account for users entering consecuative numbers spaced apart //inputing "8 9" will output "Please enter number 9: Please enter number 10:" int main() { for(int i = 8, val; i < 24; i++) { cout << "Please enter number " << i << ": "; cin >> val; if (val == i) { continue; //if the input is valid, continue with the loop } //end if if (val != i) { cout << endl << "Wrong number entered!" << endl << "You entered " << val << " where you were meant to enter " << i << "." << endl << "Try again." << endl << endl; --i; } //end if } //end for return 0; } #include using namespace std; // Exercise 2: Write a program prompting the user for all numbers from 8 to 23, inclusive. // Must use a for loop. int main(int argc, char **argv) { const int START = 8; const int END = 23; // Declared only once for efficiency. int n; for (int i = START; i <= END; i++) { for (;;) { cout << "Enter the number " << i << ": "; cin >> n; if (cin.fail()) { cout << "Invalid response. Not a number.\n"; cin.clear(); cin.ignore(99999, '\n'); } else if (n == i) { break; } else { cout << "Invalid response: incorrect input.\n"; } } } cout << "Congratulations!\n"; return EXIT_SUCCESS; } //Another Solution #include int main() { int v; std::cout << "Type the integers between 8 and 23, both included" << std::endl; for (int i = 0; i <= 23; i++) { if(i>=8 && i <= 23) { std::cin >> v; if (v != i) { i--; std::cout << "That's not the right number." << std::endl; } if (i == 23) { std::cout << "You did it!" << std::endl; } } } } // By Víctor Matía Rodríguez, Spain #include // std::cout, std::cin using namespace std; int main() { // The user input int input; // Ask for a number cout << "Please, enter a number: "; // Get the user input cin >> input; // While the user is not inputting a correct number... while (cin.fail()) { // Ask again for input cout << "Please, enter a valid number: "; // Clear the input we got before cin.clear(); // Ignore the new line character cin.ignore(256, '\n'); // Get the input again cin >> input; } // If the input is [56, 78], YOU WIN if (input >= 56 && input <= 78) cout << endl << "YOU WIN" << endl; else // Else YOU LOSE cout << endl << "YOU LOSE" << endl; return 0; } #include int main(int argc, char** argv){ for(int i = 0; i < 1; i++){ std::cout << "Will you type all of the numbers between 8 and 32?\n"; } return 0; } Failed solutions (correct them as an extra exercise) #include using namespace std; int main() { int a = 8; int number; cout << "Enter all the numbers from 8 - 23.\n"; for(; a < 24;) { cin >> number; if(number == a) { a++; cout << "Next number:\n"; } else cout << "? "; } return 0; } This program fails to comply fully with the requirements. (please correct this as an extra exercise) /*EXERCISE 2 Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. */ #include "stdafx.h" #include #include using namespace std; int main(){ cout << "Insert numbers from 8 to 23, both included.\n"; for (int i = 8; i <= 23; ++i){ string suffix; if (i == 8) suffix.assign("st"); else if (i == 9) suffix.assign("nd"); else if (i == 10) suffix.assign("rd"); else suffix.assign("th"); cout << "Insert the " << i - 7 << suffix << " number.\n"; int inp; cin >> inp; if (inp != i){ do { cout << "Wrong answer, you must enter: " << i << "\n"; cin >> inp; } while (inp != i); }else{} } cout << "You did it!\n"; system("pause"); } /*EXERCISE 2 Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. alternative solution by Chaitali Kulkarni.. :) */ # include using namespace std; # define max 100 int main() { int n,i; for(i=0;i>n; if(n<8||n>24) cout<<"\nOops entered value is not between 8-24!! !Please enter it again.."; }while(n<8||n>24); cout<<"\nN= "< /* Exercise : you have to enter all number from 8 to 23 . Season (08/18/2014). */ #include using namespace std; int main(){ int num,temp[20]; bool flag=true; cout << "Let enter the number from 8 to 23 !" << endl; for(int i=0;i<(23-8);) { cin>>num; if(8<=num && num <=23){ for(int j=0;j<20;j++){ // check number if(temp[j]==num){ flag =false; } } if(flag){ cout<<"good :"<< num << " go on \n"; temp[i]=num; i++; }else{ cout<<"The number already entered before ! \n"; flag=true; } }else cout<<"Bad ! try again \n"; } cout<< "\n"<< "You have finished"; return 0; } Same as Exercise 2 but you must use a while. Solution #include using namespace std; int main() { int input, arr[16] = 0, i = 0; bool duplicate = false; cout << "Please enter integers between 8 and 23: " << flush; do { cin >> input; if (input >= 8 && input <= 23) { for (int j = 0; j <= i; j++) { if (input == arr[j]) { duplicate = true; } } if (duplicate) { cout << "Sorry the integer is already entered, try another one: " << flush; duplicate = false; } else { arr[i] = input; cout << "Correct, please proceed to the next integer: " << flush; i = i + 1; } } else { cout << "Incorrect, please enter integers between 8 and 23: " << flush; } } while(arr[16] == 0); return 0; } //Judy Mok sigurrosist@gmail.com //C++ Exercise 20161103 //Write a program that asks the user to type all the integers between 8 and 23 (both included) using a for loop. include "stdafx.h" include using namespace std; int main() {int number = 8, correct = 8; while (correct <=23) { cout << "Enter the number " << number << " : " ; cin >> number; while (number != correct) {cout << "Enter the number " << correct << " again : " ; cin >> number;} if (number == correct) {number = 1 + number; correct = 1 + correct;} } cout << "Good job" < using namespace std; int main( ) { int num, i = 8; cout<<"enter numbers from 8 to 23\n"; while(i != 23) { cin >> num; if(num == i) { cout <<"continue"; i++; } else { cout << " wrong!! the next number was " < #include int i,j,k,p,n[16]; main(){ clrscr(); for(i=8;i<25;i++){n[i-8]=i;} while(k<16){ cout<<"The program will end once all required numbers were all given.\nEnter numbers between 8 and 23 (both inclusive): "; cin>>p; for(j=8;j<25;j++){ if(n[j-8]==p){k++;n[j-8]=0;} } clrscr(); } getche(); return 0; } //By Andrea ~ OmegaTM #include int main() { int number, cont=8; while(cont <= 23) { std::cout << "Enter a number: "; std::cin >> number; if(number != cont) { std::cout << "Error!\n"; cont--; } cont++; } return 0; } //solution by Afrim Kamberi #include using namespace std; int main() { int n = 8; cout << "current number is 8" << endl; int i = 8; while(i<=23) { cout << "put the following number"; cin >> i; n += 1; if (i != n) { cout << "wrong entry,try again" << endl; i -= 1; n -= 1; } } return 0; } #include using namespace std; int main() { short int input, i(8); // Input and loop variables. while (i <= 23) { cout << "Enter the number " << i << ": "; cin >> input; i += (input == i); // Only add to the loop if input is correct } return(0); } Alternative solutions ////Alternate solution by Gunnar Godara #include using namespace std; void main() { int i=8, num; while(i<=23) { cout<<"enter the value = "; cin>>num; if(num==i) { i++; } else { cout<<"You entered wrong number"; } } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// A solution that uses input from the user could be #include //Write a program that asks the user to type all the integers between 8 and 23 (both included) using a while loop. @@@ By Hamid N using namespace std; void main() { unsigned int num[30]; int i=8; cout<<" dear user type every integer between 8 and 23"<>num[8]; do{ if(num[i]!=8 && num[i]!=i) { cout<<"\n\n\n the enteger must begin from 8 and accending ! ERROR ! "; break; } cout<<"\n\n you entered :"<>num[i]; }while(num[i]>=8 && num[i]<=23); } } //alternate solution by Nathaniel Demandaco #include using namespace std; int main(){ int num, i=8; cout<<"Type all numbers from 8 to 23\n"; cout<<"start now: "; while (i<=23){ cin>>num; cout<<"next: "; if (num!=i){ cout<<"Invalid number, next is: "< //Write a program that asks the user to type all the integers between 8 and 23 //(both included) using while. int main() { int a,b; start: cout<<"\nEnter numbers from 8-23"; cin>>b; while(b>=8&&b<=23) { cout< using namespace std; int main() { int typedInt = 0; int i = 8; cout << "Enter the numbers between 8 and 23: " << endl; while(i <= 23) { cin >> typedInt; if(typedInt != i) { cout << "You missed a number in the sequence " << i << endl; } else { i++; } } } /*EXERCISE 3 ~asdfk Write a program that asks the user to type all the integers between 8 and 23 (both included) using a "while" loop. */ #include using namespace std; void main() { int num, next(8); while (next < 25) { cout << "Please enter a number: "; cin >> num; if (num == next) { next++; }else { cout << "Wrong input, actual number: " << next << endl; system("pause"); return; } } cout << "Congratulations\n"; system("pause"); } # include int main () { using std::cout; using std::cin; int i = 8; int num; while (i <= 23) { cout<<"Enter the integer again\n"; cin>>num; i++; } return 0; } /*EXERCISE 3 ~asdfk Write a program that asks the user to type all the integers between 8 and 23 (both included) using a "while" loop. */ // Alternative solution by Metalhands #include "stdafx.h" #include void main(){ int i = 8; using namespace std; while (i <= 23){ cout << "Enter nmber:" << endl; int flag; cin >> flag; if (flag != i) { cout << "Error!!! Try again" << endl; continue; } else cout << "Valid number entered: "<< flag << endl; i++; } } An easy solution :) By Francis Percival D. Wisco—Kaykiks<3 /* EXERCISE 3 */ #include #include using namespace std; main() { int A = 8; int B; while (A >= 8 && A<=23) { cout<<"Enter the number "<< A<< ":"; cin>> B ; if(A == B) { A = A + 1; } } getch(); return 0; } /* Easy Max and Min solution. Ankit Nayak*/ #include using namespace std; int main(){ int max, min; max=23; min=8; int capture; int interval=0; while(interval<(max-min+1)){ cout<<"Enter number 8 till 23 serially: "; cin>> capture; if(capture==(min+interval)){ cout<<"Correct entry"< //mostafa jawad using namespace std; void main() { int n,sum=0,c=1; while(c<=10) { cout<<"ENTER NUMBER "<>n; cout< using namespace std; int main() { int ival; cout << "please enter integer between 8 and 23: " << endl; cin >> ival; while (ival >= 8 && ival <= 23) cin >> ival; return 0; } /* Exercise : you have to enter all number from 8 to 23 . Season (08/20/2014). */ #include using namespace std; int main(){ int num, temp[20],i=0; bool flag = true; cout << "Let enter the number from 8 to 23 !" << endl; while (i<=(23-8)){ cin >> num; if (8 <= num && num <= 23){ for (int j = 0; j<20; j++){ // check number if (temp[j] == num){ flag = false; } } if (flag){ cout << "good :" << num << " go on \n"; temp[i] = num; i++; } else{ cout << "The number already entered before ! \n"; flag = true; } } else cout << "Bad ! try again \n"; } cout << "\n" << "You have finished"; return 0; } // By Víctor Matía Rodríguez, Spain #include // cout, cin using namespace std; int main() { // The user input int input; // Explain the exercise cout << "Please, enter every number from 8 to 23." << endl << endl; // Loop from 8 to 23 (could also be i <= 23) int i = 8; while (i < 24) { // Ask for the number to be entered cout << "Please, enter the number " << i << ": "; // Get the user input cin >> input; // If the input isn't a number or the number is not the correct one while (cin.fail() || input != i) { // Ask the user to input the correct value cout << "Please, enter the correct value, the number " << i << ": "; // Clear the input we got before cin.clear(); // Ignore the new line cin.ignore(256, '\n'); // Get the user input again cin >> input; } // Add a new line to make it look kind of better cout << endl; i++; } // Congratulate the user cout << "Congratulations, you have correctly entered every number for " << "the range [8, 23]!" << endl; return 0; } //By Alex Lobo Rodrigues // Example program #include int main() { int v; int i = 8; std::cout << "Type the integers between 8 and 23, both included" << std::endl; while (i != 24) { if(i>=8 && i <= 23) { std::cin >> v; if (v != i) { std::cout << "That's not the right number." << std::endl; }else{ i++; } if (i == 24) { std::cout << "You did it!" << std::endl; } } } } Write a program that asks the user to type 10 integers and writes the sum of these integers. Solution // Example 1 #include using namespace std; int main() { int input, sum=0; for (int i = 0; i < 10; i++) { cout << "Please enter integer " << i+1 << ": " << flush; cin >> input; if (cin.fail()) { while (cin.fail()) { cout << "Please do not enter values other than integer, try again: " << flush; cin.clear(); cin.ignore(256,'\n'); cin >> input; } sum = sum + input; } else { sum = sum + input; } } cout << "The sum of the integers is " << sum << endl; } //Made by: Reiz Troy D. Durante /// By : Sardor Karimov #include using namespace std; int main() { int sum = 0,x = 0; cout<<"enter 10 integers\n"; for(int i = 0 ; i <= 9; i++) { cin>>x; sum += x; } cout< using namespace std; int main() { int num,sum=0; for (int i=0;i<10;i++) { cout<<"Enter number: "; cin>>num; sum+=num; } cout< using namespace std; int main() { int num[10],sum=0; for (int i=0;i<10;i++) { cout<<"enter a number: "; cin>>num[i]; sum+=num[i]; } cout<<"The sum of all these numbers is "< using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cout << "Enter 10 integers:\n"; int n, total=0; for(int i=0; i<10; i++) { cout << "#" << i+1 << "> "; cin >> n; total+=n; } cout << "Total: " << total << endl; return 0; } #include using namespace std; int main() { int sum=0; int temp=0; cout << "Enter 10 integers and i will write the sum of them.\n"; for (int loop=1; loop<=10; loop++) { cout << "Integer #" << loop << ": "; cin >> temp; sum += temp; } cout << "The sum of all the integers entered is " << sum << ". "; } //alternate solution by Gunnar Godara #include using namespace std; void main() { int i ,num, sum=0; for(i=1;i<=10;i++) { cout<<"enter value = "; cin>>num; sum += num; } cout<<"Sum of all the numbers is = "<< sum ; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /* A program to sum 10 integers using a loop */ #include using namespace std; int main() { //Define variables int i = 1; //Counter int n = 0; //Sum int temp; //Input store cout << "This program sums ten user entered integers\n\n"; for (i = 1 ; i <= 10 ; i++) { cout << "Type integer " << i << ": "; cin >> temp; n = n + temp; } cout << "\nThe sum of the integers is: " << n << endl; return 0; } //alternate solution by Nathaniel Demandaco #include using namespace std; int main(){ int input,temp=0; cout<<"this program will add ten numbers you input.\n"; for(int i=1;i<=10;i++){ cout<>input; temp=temp+input; } cout<<"the sum of 10 numbers is: "< using namespace std; int main() { int i,s=0,x; for(i=0;i<10;i++) { cout<<"Type an integer: ";cin>>x; s=s+x; } cout<<"The sum is : "< int main() { int i, num, sum = 0; for(i = 0; i<10; i++) { printf("Integer: "); scanf("%d", #); sum += num; } printf("The sum is %d\n", sum); return 0; } A possible solution using an array. #include using namespace std; int main() { int numbers[10], total=0; cout << "Please enter 10 integers. " << endl; for ( int n=0,c=1; n<10; n++ ) { cout << c << ". "; cin >> numbers[n]; c++; total+=numbers[n]; } cout << endl << "Total = " << total; return 0; } #include using namespace std; int main() { int countingSum = 0; int currentNum = 0; cout << "Type any 10 numbers and ill add them up for you..." << endl; for(int i = 0; i < 10; i++) { cin >> currentNum; countingSum += currentNum; } cout << "Total: " << countingSum << endl; system("pause"); } A possible solution using a vector. #include #include using namespace std; int main() { vector numbers; do { short int input; static short int count = 1; cout << "Enter the " << count << "(st/nd/rd/th) value: "; if (! (cin >> input)) { cin.clear(); cin.ignore(10000, '\n'); } else { numbers.push_back(input); count += 1; } } while (numbers.size() != 10); int total; for(int & n : numbers) { total += n; } cout << total << endl; return 0; } /*EXERCISE 4 Write a program that asks the user to type 10 integers and writes the sum of these integers. */ #include using namespace std; void main() { int num, sum(0); for (int i = 1; i < 11; i++) { cout << "Input number " << i << ": "; cin >> num; sum += num; } cout << "Sum of all your ten numbers are: " << sum << endl; system("pause"); } /* Write a program that asks the user to type 10 integers and writes the sum of these integers. By Eliel Garcia */ #include using std::cout; using std::cin; using std::endl; const int MAX_ELEMENTS_IN_ARRAY = 10; int getUserInput(int userInputArray[]); int inputFail(int arrayPosition, int userInputArray[]); int main() { int arraySum = 0; int userInputArray[MAX_ELEMENTS_IN_ARRAY]; getUserInput(userInputArray); for(int i = 0; i < 10; i++) { arraySum += userInputArray[i]; } cout << arraySum; cin.get(); return 0; } int getUserInput(int userInputArray[]) { for(int i = 0; i < 10; i++) { cout << "Enter number " << i+1 << endl; cin >> userInputArray[i]; inputFail(i, userInputArray); } } int inputFail(int arrayPosition, int userInputArray[]) { while(!cin) { cin.clear(); cin.ignore(3000, '\n'); cout << "Invalid character, please try again " << endl; cin >> userInputArray[arrayPosition]; } } #include using namespace std; void main() { int sum=0,num[10]; cout<<"enter the up to 10 number\n"; for(int i=0; i<10; i++) { cout<<"num"<<"["<>num[i]; sum+=num[i]; } for(int i=0; i<10; i++) { cout< \\ By metalhands include "stdafx.h" include void main(){ using namespace std; int i = 1; int sum = 0; while ( i <=10 ){ cout << "Enter number position " << i << ": "; int y; cin >> y; sum = sum + y; i++; } cout << "Final sum is: " << sum << endl; } /* Exercise : you have to enter 10 integers number.The program will auto return the sum of these Season (08/20/2014). */ #include using namespace std; int main(){ int sum=0,num; cout << "Let enter 10 integers number ,the program will return sum of these" << endl; for (int i = 0; i < 10; i++){ cout << "number " << i+1 << " :"; cin >> num; sum += num; } cout << "\n" << "The sum :" << sum< using namespace std; int main() { cout<<"input 10 integrers"<>n; sum=sum+n; } cout<<"sum is "< using namespace std; int main(){ int a,b,c; c=0; cout<<" Enter any ten Number \n"; for(a=1;a<=10;a++){ cout<>b; c+=b; // or c=c+b } cout< using namespace std; int num[10] ; int typeNum = 1; int main() { cout << "Type in 10 numbers "<> num[i]; typeNum++; } cout << "Numbers you type "; for (int n = 0; n <= 9; n++) { cout << num[n] << " "; } cout << endl; system("pause"); return 0; } // By Víctor Matía Rodríguez, Spain #include // cout, cin using namespace std; // The main function int main() { // The user input int input; // The sum (make it big) long int sum = 0; // Loop 10 times for each digit for (int i = 0; i < 10; i++) { // Ask for the i + 1 integer cout << "Please, enter the integer number " << i + 1 << ": "; // Get the user input cin >> input; // If the input isn't correct while (cin.fail()) { // Ask for a valid number cout << "Please, enter a valid integer number for the value " << "number " << i + 1 << ": "; cin.clear(); // Clear the input we had received cin.ignore(256, '\n'); // Ignore the new line cin >> input; // Get the user input again } sum += input; // Add the input to the sum value, adding every time } // Show the outcome of the sum cout << endl << "The sum of the 10 digits you entered is " << sum << endl; return 0; } //By Michaelo Angelo #include int nTemp; int nSum; void main() { std::cout << "Enter ten intenegers: " << std::endl; for (int i = 0; i < 10; ++i) { std::cin >> nTemp; nSum = nSum + nTemp; } std::cout << "Sum of your numbers is: " << nSum << std::endl; system("Pause"); } //By Jonathan Nunez #include #include #include using namespace std; #define newline cout<<'\n' void process(void); int main() { printf("Enter 10 integers: ");newline; process(); system("pause"); return 0; } void process(void) { string user_input; int sum(0), cc; for (int i = 0;i < 10; i++) { cout << i << ") "; getline(cin, user_input); stringstream(user_input) >> cc; sum += cc;//sum = 0 }newline; cout << "Sum of all integers: " << sum; newline; } Author: jimmymass; Write a program that asks the user to type 10 integers and writes the smallest value. Solution /** * @author Luiz Erivan M. S. Filho */ #include using namespace std; int main(int argc, char const *argv[]) { int num, smallest = 0; cin >> num; smallest = num; for(int i = 0; i < 9; i++){ cin >> num; if(num < smallest) smallest = num; } cout << smallest << endl; } // Example #include using namespace std; int main() { int arr[10], temp; for (int i = 0; i < 10; i++) { cout << "Please enter integer " << i + 1 << " : " << flush; cin >> arr[i]; for (int j = 0; j < i; j++) { if (arr[i] < arr[j]) { temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } } cout << "The smallest number is: " << arr[0]; return 0; } //Made by Lam P. #include using namespace std; int main() { int A[10]; for(int i=0;i<10;i++) { cin>>A[i]; } int min; min=A[0]; for(int i=0;i<10;i++) { if(min>A[i]) min=A[i]; else min=min; } cout< int main () { int number, min; for (int i = 0; i < 10; i++) { if (i == 0) { std::cout << "Number " << i+1 << " "; std::cin >> number; min = number; } else { std::cout << "Number " << i+1 << " "; std::cin >> number; } if (number < min) { min = number; } } std::cout << "The smallest value is " << min << std::endl; return 0; } //Made by Mustafa Aga Albanian. #include using namespace std; int main() { int i, min=999; int t[10]; cout << "Write 10 integers:" << endl; for (i=1; i<10; i++) { cin>> t[i]; } for (i=1; i<10; i++){ if (min>t[i]) min=t[i]; } cout <<"The smollest value is: " << min << endl; return 0; } //Made by: Reiz Troy D. Durante #include #include int i,n,p=2147483647; main(){ clrscr(); while(i<10){ cout<<"Enter 10 integer: \n"; cin>>n; if(n using namespace std; int main() { int num[10],min; for (int i=0;i<10;i++) { cout<<"enter 10 numbers: "; cin>>num[i]; } min=num[0]; for (int i=0;i<10;i++) if(num[i] using namespace std; main() { int counter,value,min_value; cout << "Enter 10 values and I tell you which is the smallest one.\n\n"; counter=1; cout << "\n1-\t"; cin >> value; min_value=value; while(counter<10) { counter++; cout << endl << counter << "-\t"; cin >> value; if(value using namespace std; int main() { int i, ppt, x; for (i = 0; i < 10; i++) { cout << "Type an integer: "; cin >> x; if (i == 0) ppt = x; else if (x < ppt) ppt = x; } cout << "The lesser value is: " << ppt << endl; return 0; } /*Solution #2 */ #include #include #include using std::cin; using std::cout; using std::vector; int main() { double input; vector vec; cout << "Enter 10 separate integers:\n"; for (int i = 0; i < 10; ++i) { cin >> input; vec.push_back(input); } sort(vec.begin(), vec.end()); cout << "Smallest value is " << vec[0]; return 0; } //Possible solution using a vector and min_element() #include #include #include using namespace std; int main() { vector inputVals; // Create vector for the input values do { short int input; static short int count = 1; cout << "Enter the " << count << " value: "; if (! (cin >> input)) { cin.clear(); cin.ignore(10000, '\n'); // This if statement is used for ensuring only numbers are entered and clearing any invalid characters } else { inputVals.push_back(input); count += 1; // adds the input value to the vector and increase the count by 1 } } while (inputVals.size() != 10); int min = *min_element(inputVals.begin(), inputVals.end()); // assigns the minimum value to min using the min_element function from algorithm cout << min << endl; // print the minimum value to the console return 0; } /*EXERCISE 5 Write a program that asks the user to type 10 integers and writes the smallest value. */ // This program will get 10 numbers from user and will determine the lowest number #include using namespace std; int main() { int num[10]; int i = 0; int small=0; cout << " please enter 10 numbers as random , this program returns the smallest one "; cout << "\n\n start now :"; cout << "\n\nEnter the number "<> num[0]; small = num[0]; for(i = 1; i < 10; i++) { cout << "\n\nEnter the number "<< i + 1 <<" of your 10 numbers "; cin >> num[i]; if (num[i] < small) { small = num[i]; } } cout << "\n\nThe smallest number is :" << small; return 0; } /*EXERCISE 5 Write a program that asks the user to type 10 integers and writes the smallest value. */ #include using namespace std; int main() { int iNumber = 0; int iSmallest = 2147483647; for (short i = 1; i <= 10; i++) { cout << "Enter number " << i << ": "; cin >> iNumber; if (iNumber < iSmallest) iSmallest = iNumber; } cout << "The smallest number is: " << iSmallest; return 0; } /* By : Sebastian Gherhes 04/08/2016 Similar to something above, but using a for loop */ #include using namespace std; int main(){ int value = 0, min = 0; cout<<"Enter 10 values and I will tell you which one is the smallest"<> value; min = value; for(int i =0; i < 9; i++){ cout<<"Enter number: "<>value; if(value < min){ min = value; } } cout<<"Smallest value is: "< #define MAX_VALUES 10 int main(int argc, char *argv[]) { int values[MAX_VALUES]; int i, min; printf("Please input 10 integers...\n"); for (i = 0; i < MAX_VALUES; i++) { scanf("%d", &values[i]); } min = values[0]; for (i = 0; i < MAX_VALUES; i++){ if (values[i] < min) min = values[i]; } printf("Min is %d\n", min); return 0; } /*EXERCISE 5 Write a program that asks the user to type 10 integers and writes the smallest value. */ #include using namespace std; int main() { // solution by Minwoo Ju,8NX cout << "this program calculates the smallest value between 10 intergers." << endl; int num[10]; int small = num[0]; for(int i = 0;i <= 10;i++) { cin >> num[i]; // gets all the values from the user. } for (int i = 0;i <= 10 ; i++){ if (small > num[i]) { small = num[i]; // analyzes the array until the smallest value is found. } } cout << "the smallest value of them all is:" << small << endl; system("pause"); // program successful. return 0; } // Ternary abuse #include int main() { int num, smallest; std::cout << "Enter 10 numbers:" << std::endl; for (int i = 0; i < 10; i++) { std::cin >> num; (i == 0) ? smallest = num : ((num < smallest) ? smallest = num : NULL); } std::cout << "Smallest number: " << smallest << std::endl; } /* Write a program that asks the user to type 10 integers and writes the smallest value. //Eliel Garcia */ #include #include using std::cin; using std::endl; using std::cout; int getUserInput(int arrayOfInts[]); void getMinInteger(int arrayOfInts[]); void inputFailControl(int arrayPosition, int arrayOfInts[]); int main() { int arrayOfInts[10]; getUserInput(arrayOfInts); getMinInteger(arrayOfInts); cin.get(); return 0; } void getMinInteger(int arrayOfInts[]) { double min = INT_MAX; for(int i = 0; i < 10; i++) { if(min > arrayOfInts[i]) { min = arrayOfInts[i]; } } cout << min; } int getUserInput(int arrayOfInts[]) { for(int i = 0; i < 10; i++) { cout << "Type integer number " << i+1 << ": " << endl; cin >> arrayOfInts[i]; inputFailControl(i, arrayOfInts); } } void inputFailControl(int arrayPosition, int arrayOfInts[]) { while(!cin) { cin.clear(); cin.ignore(3000, '\n'); cout << "Invalid character, please try again " << endl; cin >> arrayOfInts[arrayPosition]; } } //Solution Provided By Neeraj Morar #include #include using namespace std; int main() { int nums[10]; for (int i = 1; i <=10; i++) { cout << "Type in an integer: "; cin >> nums[i]; cout << "\n"; } sort(nums, nums + 9); cout << nums[0]; cout << "\n"; cout << "\n"; system("PAUSE"); } //solution by adel deli #include using namespace std; int main() { cout <<"Enter 10 numbers to determinate the smallest value: "<< endl<< endl; int num, temp; int min; cout<< "Enter number 1: "; cin >> num; min = num; for(int i = 2; i <= 9; i++){ cout << "Enter number "<> num; if (num < min){ min = num; } } cout <<"The smallest number is: "<< min; return 0; } /* EXERCISE 5 : enter 10 intergers and return the smallest number . Season (09/3/2014). */ #include using namespace std; int main() { int i, arg[10],min; for (i = 0; i < 10; i++) { cout << "Let enter the number " << i+1 << " :"; cin >> arg[i]; cout << endl; if (i == 0) min = arg[0]; if (arg[i] < min) min = arg[i]; } cout << "string number :"; for (i = 0; i < 10;i++) cout << arg[i] << "\t"; cout << endl; cout << "The smallest number is : " << min << endl; return false; } // By Víctor Matía Rodríguez, Spain #include // cout, cin using namespace std; // The main function int main() { // The user input int input; // The array that will hold every integer int inputs[10]; // A size of 10 for 10 inegers // The smallest integer int smallest; // Loop 10 times, one per digit for (int i = 0; i < 10; i++) { // Ask for the i + 1 integer cout << "Please, enter the integer number " << i + 1 << ": "; // Get the user input cin >> input; // If the input isn't correct while (cin.fail()) { // Ask for a valid number cout << "Please, enter a valid integer number for the value " << "number " << i + 1 << ": "; cin.clear(); // Clear the input we had received cin.ignore(256, '\n'); // Ignore the new line cin >> input; // Get the user input again } // Set the corresponding value in the inputs array with the input value inputs[i] = input; } // Set a default value for smallest smallest = inputs[0]; // Iterate through each number of the array except the first one that we // already used right before // Remember sizeof returns the number of bytes, so we divide it by the // number of bytes in the first value and we'll get the real // array size (10) for (int i = 1; i < sizeof(inputs) / sizeof(inputs[0]); i++) { // If any of the new numbers is smaller than the first number if (inputs[i] < smallest) smallest = inputs[i]; // Set such number as the smallest one } // Inform the user about the smallest number cout << endl << "The smallest value you have entered is " << smallest << endl; return 0; } /*By Jonathan Nunez*/ #include #include #include using namespace std; namespace vars { float input; string user_input; int min = 10, max = 0;
{'wiki_source': 'wikibooks', 'other_info': "{'content_model': 'wikitext', 'language': 'en', 'title': 'C++ Programming/Exercises/Iterations/Pages', 'type': 'text'}"}