finding numbers between 2 numbers [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I just started learning c++ and very new to it and trying to write a simple for loop to print all numbers between two numbers.
e.g 1-4
numbers between 1,4
output
2
3
the for loop.
int main() {
int firstNumber;
int secondNumber;
std::cout << "Enter first number" << std::endl;
std::cin >> firstNumber;
std::cout << "Enter second number" << std::endl;
std::cin >> secondNumber;
for (int i=firstNumber; i<secondNumber; i++) {
std::cout << i << std::endl;
}
}

At the first step of for loop execution, i++ is not applied - and i is still equal to 1 (its initial value, defined in int i = firstNumber statement. Quoting the doc:
for (initialization; condition; increase) statement;
It works in the following way:
initialization is executed. Generally it is an initial value setting
for a counter variable. This is executed only once.
condition is
checked. If it is true the loop continues, otherwise the loop ends and
statement is skipped (not executed).
statement is executed. As usual,
it can be either a single statement or a block enclosed in braces { }.
finally, whatever is specified in the increase field is executed and
the loop gets back to step 2.
In your case you can just start the loop from firstNumber + 1.

Why not just set i to 2 to start the loop?

Related

How to display all even number that are smaller or equal to the number inserted [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 months ago.
Improve this question
How to create a code in c++ that if I insert any number "n" it displays all the "even" numbers that are smaller or equal to "n"
I dont have much knowledge in C++ so I would appreciate the help
This is what I tried and think it's good enough
int n;
cout << "\nEnter number= ";
cin >> n;
cout << "\nEven numbers are ";
for(int i = 0; i <= n; i++)
{
if ( i % 2 == 0 )
{
cout << i <<" ";
}
}
You can use a repetition loop to check for all the numbers until N and the look if each number is divisible by 2 (when, in that division, the remainder is 0).
#john ideia of starting counting at 2 and going up 2 at a time is a easier one.

For loop displaying incorrect information stored inside array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have this really simple program, with a for loop designed to run 3 times.
For every iteration, it will ask the user to enter in the weather. Each time, the user's input is stored in an integer array called C.
Once the loop concludes, I have another for loop that print's out the user's input. This next loop works fine for the 2 values, but gives off some weird messed up value once it reaches the third iteration.
int main(){
//Variable declaration:
int days;
int C[2];
int F[2];
for(days=0; days<3; days++){
cout << "What is the temperature in celsius for day " << days + 1 << ":" << endl;
cin >> C[days];
F[days] = (C[days] * 9/5) + 32;
}
cout << "\nCelsius\t\t Farenheit\n-------\t\t ---------" << endl;
for(days =0; days <3; days++){
cout << C[days] << "\t\t " << F[days] << endl;
}
return 0;
}
This next loop works fine for the 2 values but gives off some weird messed up value once it reaches the third iteration.
Your definition of C states it has 2 entries, but your loop runs for
three iterations (0, 1, and 2).

If statement not excepting line of text? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This code should ask for in put with "What is If (selection)?"
and this part it does. but once you input the answer as "Provides the ability to either do a block of code or skip that block of code." the out come should be "Correct!" but instead it either asks to press key to end or re asks the question. does anyone have and advice as to how i could fix this?
srand((unsigned)time(0));
int random_interger;
int lowest = 2, highest = 18;
int range = (highest - lowest) + 1;
for (int index = 0; index < 20; index++) {
random_interger = lowest + int(range*rand() / (RAND_MAX + 1.0));
if (random_interger == IF) {
cout << " What is If (selection)?" << endl;
cin >> IFs;
if (IFs == "Provides the ability to either do a block of code or
skip that block of code.") {
cout << "Correct!" << endl;
}
string IFs;
cin >> IFs;
Even if the user types exactly the long line you expect (he won't), this wouldn't work. cin >> into a string will read just one word. You would get only "Provides".
Look up the getline API. But even then, I doubt anyone would type those lines exactly.

Taking an unspecified number of inputs in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I came across a question whereby you are told there will be unspecified number of queries hence you got to keep on taking input for this unspecified number of queries
all i know is that in c++ or even in another programming language, when the program needs to take unspecified number of input, we prompt the user to enter a certain value which will be used to terminate the infinite loop e.g
for (;;)
{
cout<<"enter 0 to stop taking input"<<endl;
int value;
cin>>value;
if (value==0)
{break;}
}
my question is how will i handle the question stating the input will be unspecified and its in an online environment
for this kind of problem you will use a loop control variable and a while loop for user input validation
while loop is use for input validation while for loop is use for specific
number of times only and it is not really suggested for unknown number of times
same with while loop, do while loop can also perform input validation the
only difference is, it will prompt the user first before evaluating the test condition
int myNum; // this is the loop control variable
cout << "enter a number, enter 0 to stop taking input " << endl;
cin >> myNum;
while(myNum!=0)
{
cout << "cograts you did not enter zero digit" << endl;
cin >> myNum;
}
use while (cin >> a) :
to see how if it works create an input file 1.in and write a bunch of numbers in it, then pipe it to your executable fle ./a.out <1.in
#include <iostream>
using namespace std;
int main() {
int a;
while (cin >> a) {
cout << a << "\n";
}
return 0;
}

Nested loops and modulus c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am learning C++ and I have a problem with my program. It should print out following if n=11:
*---------*
-*-------*-
--*-----*--
---*---*---
----*-*----
-----*-----
----*-*----
---*---*---
--*-----*--
-*-------*-
*---------*
This is my code, which works correctly with n=5, but not with greater numbers:
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter size (n x n): " << endl;
cin >> n;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (i%n==j%n) cout << '*';
else if (i%(n-i)==j%(n-j)) cout << '*';
else cout << '-';
}
cout << endl;
}
return 0;
}
This is being printed out if n=11:
*---------*
-*----*--*-
--*-----*--
---*---*---
----*------
-----*-----
-*----*--*-
---*---*---
--*-----*--
-*----*--*-
*---------*
I see that I have successfully wrote how to print out one of '*' diagnoles. But something isn't working with other one, which is going backwards.
Unfortunately, I am not being able to resolve this problem and need your advice. What am I doing wrong? How to debug such problems?
This problem is really simple to debug.
Take a look at the first erroneous *. It appears at the position with i=1, j=6. With n=11, your condition i%(n-i)==j%(n-j) becomes 1%(11-1) == 6%(11-6) which is effectively true because the expression evaluates to 1 on both sides.
What is behind this expression? Why do you use this kind of if to determine whether the cell belongs to the second diagonal? Try to write down each pair i, j which should be printed on the second diagonal, and you should notice a more simple pattern.
P.S. In the expression if (i%n==j%n) you don't have to take operands modulo n, because both of them are less than n, so it is redundant and may be rewritten simply as if (i == j).