bornerwave
Master Member
- Joined
- May 25, 2013
- Messages
- 3,621
- Reaction score
- 37
sry test.........
Last edited:
okay guys it works now , thanks all , will post more questions here
i guess i am allowed to use anything , but it will be rather obvious to my lecturer if he sees some bombastic codings lol

i guess i am allowed to use anything , but it will be rather obvious to my lecturer if he sees some bombastic codings lol
// Horizontal PLAYER 1 (left -> right) = Scissors, Paper, Stone
// Vertical PLAYER 2 (top -> down) = Scissors, Paper, Stone
// Cell [1][2] -> PLAYER 1 choose (2 = Stone), PLAYER 2 choose (1=Paper)
const int outcome[3][3] = { { 0, -1, 1 },
{ 1, 0, -1 },
{ -1, 1, 0 } };
const char const *[3] = { "This is no winner", "Player 1 is the winner", "Player 2 is the winner" };
Alright guys, here is the next question.
Write a program, using a for loop, to generate 10 random numbers between 15 and 150, inclusive. Then, the program should print how many of those numbers fall into the following categories: (1) between 15 and 50, inclusive; (2) between 51 and 100, inclusive, and (3) between 101 and 150, inclusive. Assume there is no user input for this program.
And here is my code , i need help because idk how to generate the 10 random numbers, i would appreciate if u guys guide me along to get the correct codes rather than posting your own version of codes which i do not understand usually haha
#include <iostream>
#include <string>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
int numberchanging;
for(numberchanging=15 ; numberchanging>=15 , numberchanging<=150 ; numberchanging ++)
{
if(numberchanging>=15,numberchanging<=50)
{
cout<<"Category 1 numbers are: " <<numberchanging<<endl;
}
else if(numberchanging>=51,numberchanging<=100)
{
cout<<"Category 2 numbers are: "<<numberchanging<<endl;
}
else if(numberchanging>=101,numberchanging<=150)
{
cout<<"Category 3 numbers are: "<<numberchanging<<endl;
}
}
return 0;
}
hmmm thanks , i will look into it , i totally have no clue on doing this because our lecturer have not taught us yet , this assignment only starts 2 weeks from now but im doing it by my little knowledge of C++ , so please bear with me![]()
well thats a great advice man, seems like i have a wrong approach in studying for tertiary education, i do read C++ for dummies though , does that help?
Here is the solution you might interested. Easy and simple.
Code:#include <Windows.h> #include <iostream> int main() { int number[10]; int output; std::cout << "Generating 10 random numbers between 15 and 150." << std::endl; for(int i = 1; i <= 10; ++i) { output = rand() % 135 + 15; std::cout << i << ". " << output << std::endl; number[i] = output; } std::cout << "Check if the output falls within which region." << std::endl; for(int i = 0; i < sizeof(number); ++i) { if(number[i] >= 15 && number[i] <= 50) { std::cout << "Number [" << number[i] << "] is between 15 and 50" << std::endl; continue; } if(number[i] >= 51 && number[i] <= 100) { std::cout << "Number [" << number[i] << "] is between 51 and 100" << std::endl; continue; } if(number[i] >= 101 && number[i] <= 150) { std::cout << "Number [" << number[i] << "] is between 101 and 150" << std::endl; continue; } } std::cin.get(); return 0; }
3 questions for you
- Can you define the range of the random number generated based on the code you presented ? Will it satisfy the question requirement ?
- Why are you not using if..else block and short cut using continue instead ?
- Regarding "number >= 51 && number <= 100" is the test of ">=51" necessary ?
3 questions for you
- Can you define the range of the random number generated based on the code you presented ? Will it satisfy the question requirement ?
- Why are you not using if..else block and short cut using continue instead ?
- Regarding "number >= 51 && number <= 100" is the test of ">=51" necessary ?
1. Yes, it will generate between 15 and 150.
Write a program, using a for loop, to generate 10 random numbers between 15 and 150, inclusive. Then, the program should print how many of those numbers fall into the following categories: (1) between 15 and 50, inclusive; (2) between 51 and 100, inclusive, and (3) between 101 and 150, inclusive. Assume there is no user input for this program.
2. Personal preference, I still prefer using continue rather than if..else.
3. It just depend on you to write the program, especially school projects, they are require to understand the concept instead of cutting short, since small program like this doesn't have impact on the performance.
4. Yes, I've made a mistake here, it' generates the 2nd number from number to 11th number.
My question for you is "Will it satisfy the requirement of the question?" I shall quote the question below for your convenience
Surely you have your preferences, and it goes that same for using goto and labels in C/C++ programs. However, you ought to be aware of your intended audience, whom is a newbie to development. As such, both academic and industrial reasons, there is already a else..if block control structure available in C/C++ that exactly exhibit the required control flow in the program. It should be used instead of using continue in such inappropriate manner.
I do not agree with such argument. Regardless if it's for academic reason or otherwise, performance is always part and parcel of any development exercise. While you are correct that academic reason should have more focus in concepts than performance, however where performance can be easily obtain with ease of effort like for this case, it should be exercise. Moreover the reason for why I say if these tests are unnecessary is not for performance reason. It is basically the logical reasoning that I'm discussing. If the above block already cover the possibility of less than X, then based on logical reasoning, there is no need to assume it has to be more than or equal to X since it must be so.
Therefore this question that I asked has nothing to do with performance per say. It has to do with the necessity to validate unnecessarily when it is already accounted for.
This one is pointed out by others, however my further question for you is
What does sizeof(number) evaluate to ? Have you ever run this set of code that you have shared ?
Are you sure number[10] exist ? What is wrong with using number[10] ? Would you be able to share ?
LOL..I shall remove them then, since you're not happy with my codes, why not you provide your solution instead?
Deleted, our senior member hate it. =)
#include <Windows.h>
#include <iostream>
int main()
{
int number[10];
int output;
std::cout << "Generating 10 random numbers between 15 and 150." << std::endl;
for(int i = 1; i <= 10; ++i)
{
output = rand() % 135 + 15;
std::cout << i << ". " << output << std::endl;
number[i] = output;
}
std::cout << "Check if the output falls within which region." << std::endl;
for(int i = 0; i < sizeof(number); ++i)
{
if(number[i] >= 15 && number[i] <= 50)
{
std::cout << "Number [" << number[i] << "] is between 15 and 50" << std::endl;
continue;
}
if(number[i] >= 51 && number[i] <= 100)
{
std::cout << "Number [" << number[i] << "] is between 51 and 100" << std::endl;
continue;
}
if(number[i] >= 101 && number[i] <= 150)
{
std::cout << "Number [" << number[i] << "] is between 101 and 150" << std::endl;
continue;
}
}
std::cin.get();
return 0;
}
int number[10];
...
for(int i = 1; i <= 10; ++i)
{
output = rand() % 135 + 15;
std::cout << i << ". " << output << std::endl;
number[i] = output;
}
for(int i = 0; i < sizeof(number); ++i)
{
if(number[i] >= 15 && number[i] <= 50)
{
std::cout << "Number [" << number[i] << "] is between 15 and 50" << std::endl;
continue;
}
if(number[i] >= 51 && number[i] <= 100)
{
std::cout << "Number [" << number[i] << "] is between 51 and 100" << std::endl;
continue;
}
if(number[i] >= 101 && number[i] <= 150)
{
std::cout << "Number [" << number[i] << "] is between 101 and 150" << std::endl;
continue;
}
}
#include <iostream>
using namespace std;
// go find out what is the value of macros
#define NO_OF_RAND 10
#define NO_OF_BUCKETS 3
int main(int argc, char **argv) {
int buckets[NO_OF_BUCKETS] = { 0 }; // go find out what does this initializer do
srand(time(NULL)); // go find out why this is important
cout << "Generating 10 random numbers between 15 and 150." << endl;
for (int i = 0; i < NO_OF_RAND; i++) {
int r = rand() % 136 + 15;
//printf("%d %d\n", r, (r - 1) / 50);
buckets[(r - 1) / 50]++; // so here you see if if..else blocks are even necessary
}
// final output
cout << buckets[0] << " random numbers in the range [15,50]" << endl
<< buckets[1] << " random numbers in the range [51,100]" << endl
<< buckets[2] << " random numbers in the range [101,150]" << endl;
return 0;
}
well, i have solved the 2nd question using arrays taught by my friend and some of my self study haha
here is my code for qn 2 , any improvements etc?
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
// DKSG: using all single letter variables like the above is a bad choice of naming convention.
// DKSG: due to legacy, iterators will normally use i, j, k, l etc single letters due to convenience
// DKSG: such naming is granted and widely accepted, but if the variable has specific purpose,
// DKSG: better naming conventions where other developers reading your codes can easily
// DKSG: decipher the usage and intention of the variable will be preferable.
// DKSG: I believe you can use "cntBet15n50", "cntBet51n100" as variables names.
// DKSG: This is the Camel case style naming convention with more meaningful names.
// DKSG: For legacy C/C++ naming convention, they like to use underscore(_) to separate the tokens
// DKSG: such as "cnt_bet_15n50" etc.
int numberchangingone[10];
srand ((unsigned)time(0)); //initialise random seed
for( int a=0;a<10;a++)// assign a value to each numberchangingone[] and display the value of it out
{
// DKSG: Are you sure below is answering your question of range [15,150]?
numberchangingone[a]=rand() % 135 + 15;
cout<<numberchangingone[a]<<endl;
}
for( int b=0;b<10;b++)
{
if(numberchangingone[b]<=50)
{
c++;
}
else if(numberchangingone[b]>=51 && numberchangingone[b]<=100)
{
// DKSG: Like I have mentioned in the earlier post, since your first if block already
// DKSG: accounted for X <= 50, then naturally to get into this block, X must be > 50
// DKSG: thus testing for X > 50 is not required. Testing for X <= 100 would have
// DKSG: been testing for lower bound of X > 50
d++;
}
else if(numberchangingone[b]>=101 && numberchangingone[b]<=150)
{
e++;
}
}
cout<<"The number of numbers that fall into Category 1 is: "<<c<<endl;
cout<<endl;
cout<<"The number of numbers that fall into Category 2 is: "<<d<<endl;
cout<<endl;
cout<<"The number of numbers that fall into Category 3 is: "<<e<<endl;
cout<<endl;
return 0;
}
Code:#include <iostream> #include <ctime> #include <cstdlib> #include <string> #include <sstream> using namespace std; int main() { int input=0; int count=0; int a=0; int b=0; char Plaintext[52]={'A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q' ,'R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y','Z','z'}; char Secretlanguage[52]={'S','s','T','t','U','u','V','v','W','w','X','x','Y','y','Z','z','A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i' ,'J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r'}; cout<<"Please enter your one word secret message"<<endl; cin>>input; for( int a=0;a<53;a++) { Plaintext[a]=Secretlanguage[a]; } return 0; }
Wow i googled the qns for research and found this wtf lol is this even allowed for a graded assignment??