Printing each number in the range specified by two random integers - c++

#include <iostream>
int main()
{
std::cout << " Enter the small number " << std::endl;
int v1 = 0;
std::cin >> v1;
std::cout << " Enter the large number " << std::endl;
int v2 = 0;
std::cin >> v2;
int x = 0;
while (x <= v2-1) {
x = v1+1;
++x;
}
std::cout << x << std::endl;
return 0;
}
This code gives no error but i couldn't get any output after input of two numbers. But, it works with a for loop like this;
{
for (int x = v1+1; x <= v2-1; ++x)
std::cout << x << std::endl;
return 0;
}
what is the mistake i should avoid using while loop ?

int x = 0;
while (x <= v2-1) {
x = v1+1;
++x;}
You get an infinite loop here, the value of x is always either v1+1, or v1+2.
use this:
int x = v1+1;
while (x++ <= v2-1) {
cout<<x;
}

Related

C++ function will not print array to console in reverse

I'm working on homework for my c++ class we have just started arrays. I cannot figure out why the function reverse_array() does not print to the console when the function show_array() prints to the console without an issue. I've attempted googling the issue without success. I am just beginning c++ so it could be something small that I'm overlooking,. I appreciate any help.
#include<iostream>
using namespace std;
double dbval[5];
void fill_array(int x, double dbval[]);
void show_array(int x, double dbval[]);
void reverse_array(int x, double dbval[]);
int main(){
int x = 0;
fill_array(x,dbval);
show_array(x,dbval);
reverse_array(x,dbval);
return 0;
}
void fill_array(int x, double dbval[]){
int count = 0;
for (x = 0; x < 5; x++){
cin >> dbval[x];
if(!cin){
break;
}
}
for(x = 0; x < 5; x++){
count = count + 1;
}
cout << "Entries " <<int(count);
cout <<endl;
}
void show_array(int x, double dbval[]){
for (x = 0; x<5;x++){
cout << dbval[x] << " ";
}
cout << endl;
}
void reverse_array(int x, double dbval[]){
for(x = 5; x < 5; x--){
cout << dbval[x] << " ";
}
cout << endl;
}
This loop for(x = 5; x < 5 ; x--) failed because it did not satisfy your condition the first time it checked (x = 5, but it needs to be smaller than 5 to enter the loop)
Change to this:
void reverse_array(int x, double dbval[]){
for(x = 4; x >= 0 ; x--){
cout << dbval[x] << " ";
}
cout << endl;
}

How to separate the outputs with commas? How can I improve this code to look more decent?

So after learning some Python, I decided to check out C++ and give it a try and decided to try and code Collatz Conjecture in Xcode.
Here's what I got.
#include <iostream>
int collatz() {
std::cout << "Enter a number: ";
int x = 0;
std::cin >> x;
while (x != 1) {
if (x % 2 == 0) {
x /= 2;
std::cout << x << " ";
} else {
x = (3 * x) + 1;
std::cout << x << " ";
}
}
return 0;
}
int main() {
collatz();
}
Extra Question: In Python there is condition called elif, how is it called in C++?
#include <iostream>
int collatz() {
std::cout << "Enter a number: ";
int x;
std::cin >> x;
while (x != 1) {
x = x % 2 ? x / 2 : x * 3 + 1;
std::cout << x << ", ";
}
std::cout << "\b\b \b\b" << std::endl;
return 0;
}
int main() {
collatz();
}
You can do something like this if you don't want comma at last
Add a check not to add comma for last digit
#include <iostream>
int collatz() {
std::cout << "Enter a number: ";
int x = 0;
std::cin >> x;
while (x != 1) {
if (x % 2 == 0) {
x /= 2;
std::cout << x ;
} else {
x = (3 * x) + 1;
std::cout << x ;
}
if (x!=1)
std::cout<<",";
}
return 0;
}
int main() {
collatz();
}
Output
Enter a number: 5
16,8,4,2,1
Using your original code (I see you've edited it), you can do this if you want to avoid an extra conditional and multiple calls through cout in your loop.
#include <iostream>
#include <iostream>
#include <sstream>
int collatz()
{
std::cout << "Enter a number: ";
int x = 0;
std::cin >> x;
std::string output;
while (x != 1)
{
if (x % 2 == 0)
{
x /= 2;
output += std::to_string(x) + ", ";
}
else
{
x = (3 * x) + 1;
output += std::to_string(x) + ", ";
}
}
output.replace(output.rfind(',', output.length()), 2, "");
std::cout << output.c_str();
return 0;
}
Probably the only thing that needs explaining here is output.replace(). rfind() finds the last comma, starting at the end. replace then clobbers the two characters at that position (", ").

Simple for loop I can't figure out

I'm kind of new to C++ so last night I thought of something. I want to print out numbers from 1-100 but with 10 numbers per line. I'm aware my code is below is wrong as it just prints 1-100 vertically. If anyone can shed some light to my question, it would be greatly appreciated. Thanks for reading :)
#include <iostream>
using namespace std;
int main() {
for(int x = 1; x <= 100; x++) {
cout << x << endl;
}
}
So you want to print 10 numbers, then a carriage return, and then 10 numbers, then a carriage return, and so on, correct?
If so, how about something like:
for(int x = 1; x <= 100; x++) {
cout << x << " ";
if ((x%10)==0) cout << endl;
}
Use the modulo operator % to determine if a number is a multiple of another:
for(int x = 1; x <= 100; x++) {
if( x % 10 == 0 ) cout << endl;
cout << x << " ";
}
How about
int main() {
for(int x = 1; x <= 100; x++) {
cout << x << " " ; //Add a space
if ( x % 10 == 0 ) {
cout << endl //Put out a new line after every 10th entry?
}
}
}
Print new line when it can be device by 10.
for(int x = 1; x <= 100; x++) {
cout << x << ",";
if ((x % 10) == 0) {
cout << endl;
}
}
for(int i=1; i<=100; i++) {
i%10==0 ? cout << i<<endl : cout<<i<<" ";
}

What's wrong with my C++ variable(scope related)?

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int x = 0, y = 1, k = 5;
{
int x = 1;
x = 10;
cout << "x is " << x << ", y is " << y << endl;
}
cout << "x is " << x << " and k is " << k << endl;
cout << endl;
cout << endl;
{
int x = 5;
int y = 6;
{
int y = 7;
x = 2;
}
cout << "(x, y) is : (" << x << ", " << y << ")" << endl;
}
cin.get();
return 0;
}
The output is:
x is 10, y is 1
x is 0 and k is 5
(x, y) is : (2, 6)
I think (x,y) should be (5,6). Because that's the coordinates x and y are in.
You're modifying x from the outer scope here:
{
int y = 7; // local variable
x = 2; // variable from outer scope
}
If you had said int x = 2; then you could expect to get (5,6). But you didn't.
You have assigned value 2 to x in last scope hence it's (2,6).
Not how it works, because the variable x from the outer scope was the one you changed in the inner scope, where there is no other x to hide it. Consider this example of why this is necessary:
static const size_t N = 10;
size_t i = 0;
int array[N];
while (i < 10) {
const int x = i*i;
array[i] = x;
++i;
} // We're out of the block where i was incremented. Should this be an infinite loop?
// Should array be uninitialized because this is the scope it was in and we updated it in a nested scope?

Recursion help in C++

The assignment is Design and Develop a C++ program to list the first N terms of the Fibonacci series.
The output should look like this:
N=2 1,1
N=2 1,1
N=3 1,1,2
N=4 1,1,2,3
N=5 1,2,3,5
N=6 ....
My problem is that I have written the recursive function below but I'm not sure how to format it so it outputs to screen in the manner above.
#include <iostream>
using namespace std;
//Function Prototype
int fib(int);
int main()
{
for (int x = 0; x < 15; x++)
cout << fib(x) << " ";
cin.get();
cin.get();
return 0;
}
//Definition of fib
int fib(int n)
{
//Return 1 when n is 0
if ( n <= 0 )
return 0;
else if (n == 1)
return 1;
else
return fib(n-1) + fib(n-2);
}
Could someone shed some light on how to get this accomplished?
Thank you.
if you don't care too much about efficiency, a double loop will do
for (int x = 2; x < 15; x++) {
cout << "N = " << x << " ";
for (int y = 2; y <= x; y++)
cout << fib(y) << " ";
cout << endl;
}
How to format?
You have a good start.
Try this as a next step...
for (int x = 0; x < 15; x++)
cout << x << "=" << fib(x) << " " << std::endl;
cin.get();
In my system, I can add to the cout line, compile, and review the output in < 10 seconds. Fast turn around and practice (for you) are your friends.
I would take a different approach. I'd save the already computed Fibonacci values so they are not computed them over and over again, like in a map, and than using that map to print the values.
std::map<int, int> fibs;
int fib(int const n)
{
auto p = fibs.find(n);
if(p != fibs.end())
return p->second;
int f = 1;
if (n > 1)
{
f = fib(n-1) + fib(n-2);
}
fibs[n] = f;
return f;
}
You can then loop through the computed values like this:
for(int n = 0; n < 10; ++n)
{
fib(n);
std::cout << "N=" << n << " ";
for(int i = 0; i <= n; ++i)
std::cout << fibs[i] << ",";
std::cout << std::endl;
}
Since it all does is print the fibonacci number, and the ones before, you just need to add them to your output ...
You can either have an aggregating string that you pass along, that will hold all the temp values, or just call another method that will have temp outputs. (mind you, it's not very efficient though :)
int fib_verbose(int n)
{
//Return 1 when n is 0
if ( n <= 0 )
return 0;
else if (n == 1) {
return 1;
}
else {
int smaller = fib(n-2);
int larger = fib(n-1);
cout << smaller << " " << larger << endl;
return smaller + larger;
}
}
You'll have to sort out the spaces, and formatting, but that's the gist.
Edit:
As per agbinfo comment: removed the 1 printing, and also storing the variables so we don't need to call them twice. (Still, for efficiency, look at Marius's answer :) ).
Here's an example that doesn't recompute values when calling fib for a single value. You can combine Marius's idea to compute the values once even on multiple runs.
The trick is that fib(unsigned&, unsigned) will return the previous fibonacci it has already computed.
#include <iostream>
using namespace std;
unsigned fib(unsigned& m, unsigned n)
{
if (n==0) {
return 0;
}
if (n==1) {
m = 0;
// cout << "0,"; // uncomment if sequence should start with a 0
return 1;
}
unsigned prev;
m = fib(prev, n-1);
cout << m << ",";
return m+prev;
}
unsigned fib(unsigned n) {
unsigned prev;
unsigned f = fib(prev, n);
cout << f;
return f;
}
int main() {
for (unsigned i=2; i<13; i++) {
cout << "N=" << i << " ";
fib(i);
cout << endl;
}
return 0;
}
Will printout:
N=2 1,1
N=3 1,1,2
N=4 1,1,2,3
N=5 1,1,2,3,5
N=6 1,1,2,3,5,8
N=7 1,1,2,3,5,8,13
N=8 1,1,2,3,5,8,13,21