My loop never ends...and I dont understand why. Any ideas? - c++

I am trying to find out why my loop never ends. I am trying to take two numbers, start with the smallest, and keep divid by 4 until it reaches 0.
#include<iostream>
using namespace std;
int main
{
int x, y, answer = 0;
cout << "dude enter two numbers " << endl;
cin >> x >> y;
//this is trouble statement
for (int num = x; num <= y; num++)
{
while (num != 0)
answer = num / 4;
cout << answer << " ";
}
}
return 0;
}

The condition while (num != 0) is the problem.
As, you are not modifying num in the while loop, hence the value of num would never change.
Hence, the infinite loop.
A few changes in your code will suffice :
#include<iostream>
using namespace std;
int main()
{
int x, y, answer = 0;
cout << "dude enter two numbers " << endl;
cin >> x >> y;
for (int num = x; num <= y; num++)
{
//Created a temporary variable.
int temp = num;
//All operations on the temporary variable.
while (temp != 0)
{
temp = temp/ 2;
cout << temp << " ";
}
cout<<endl;
}
return 0;
}

Related

How to print inverted half-star pyramid pattern next to each other?

I want to display an inverted half-star pyramid pattern next to each other.Here's the desired output
And here's my code:
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter Number of Rows: ";
cin >> n;
for (x = n; x >= 1; x--)
{
for (y = 1; y <= x; y++)
{
if (y <= x)
cout << "*";
else
cout << " ";
}
for (y = n; y >= 1; y--)
{
if (y <= x)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Here's the output I got after running the code.
The number of rows desired is 10.
After running my code, the output isn't like what I expected. Please tell me how to make it right.
Thank you.
I saw some symmetries in the problem
for n rows, we're printing 2*n+1 characters
for the yth row, we're printing an asterisk if x is less than n-y or more than n+y
So I coded a single double loop with the more complex if statement. I had to adjusted the if statement until it worked.
#include <iostream>
using namespace std;
int main()
{
int n, x, y;
cout << "Enter Number of Rows: ";
cin >> n;
for (y = 0; y < n; y++)
{
for (x = 2*n+1; x > 0; x--)
{
if ((x > n+y+1) || (x < n-y+1))
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
Well, you need to change your logic a little bit rest all is fine.
Here is the code:-
#include <iostream>
using namespace std;
int main()
{
int n, x, y, k;
cout << "Enter the number of Rows: ";
cin >> n;
for(x = 1; x <= n; x++)
{
for(y = n; y >= 1; y--)
{
if(y <= x)
cout << " ";
else
cout << "*";
}
for(y = 1; y <= n; y++)
{
if(y <= x)
cout << " ";
else
cout << "*";
}
cout << "\n";
}
return 0;
}
Explanation:- I am starting from row 1 and going till row "n". Now I need to print two different inverted flags so I used two for loops for that. In one loop I am starting from column "n" and going till row >1 and in the other loop, I am doing the just opposite of that so that both flags will be opposite to each other. Just try to understand this code by taking X as row and Y as column.

why do i have to add a new variable in this code

I was trying to write a code that takes two numbers as a input and change the numbers into words in certain rules.
Below is the code I wrote at first, but whatever input I put in, the loop starts from x=0.
#include <iostream>
int main() {
string nums[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int x,y;
cin >> x;
cin >> y;
for (int x = x; x <= y; x++){
if (x <= 9){
cout << nums[x] << "\n";
}
else if (x % 2 == 0){
cout << "even" << "\n";
}
else {
cout << "odd" <<"\n";
}
}
return 0;
}
Below is the second code I wrote and it worked as I wanted to.
#include <iostream>
int main() {
string nums[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int x,y;
cin >> x;
cin >> y;
for (int i = x; i <= y; i++){
if (i <= 9){
cout << nums[i] << "\n";
}
else if (i % 2 == 0){
cout << "even" << "\n";
}
else {
cout << "odd" <<"\n";
}
}
return 0;
}
I found out that if I add a variable, it works as what I wanted too. I found the solution but I don't know why I have to add a variable and why the first one always starts from x=0.
In first sample that you provided you defined a local variable(x) as same name in outer block:
for (int x = x; x <= y; x++)
In fact shadowing of variable take place here and c++ hide the declaration of variable of outer block with same name in nested block.

argument of type “int” incompatible with parameter of type “int*”

The goal is to search the array and find a specific number in it and return the position. I have the method for how to do it down but trying to get it to run in the main method is giving me the error in the title. What do i do to fix?
#include "stdafx.h"
#include <iostream>
using namespace std;
int searchArray(int a[], int x) {
for (int i = 0; i < a[100]; i++) {
if (x == a[i])
return i + 1;
break;
}
return -1;
}
int main()
{
int wait, x, y, a[100];
//problem 3
cout << "Enter the size of the array(1-100): ";
cin >> y;
for (int i = 0; i < y; i++) {
cout << "Enter an array of numbers:";
cin >> a[i];
}
searchArray(a[100], x); //i get error on this line with a[100]
cin >> wait;
return 0;
}
Expected is it should run with no errors and find position of a number in the array but I just get the error and cant run it.
After the for() loop in main(), you need something like:
cout << "Enter the value to search for: ";
cin >> x;
wait = searchArray(a, x);
cout << x;
cout << " is at position: ";
cout << wait;
int searchArray(int a[], int x)
{
for (int i = 0; i < 100; i++) //change a[100] to 100 because it only needs the size not the array itself
{
if (x == a[i])
return i + 1;
break;
}
return -1;
}
int main()
{
int wait, x, y, a[100];
cout << "Enter the size of the array(1-100): ";
cin >> y;
for (int i = 0; i < y; i++) {
cout << "Enter an array of numbers:";
cin >> a[i];
}
cout<<"Number to look for: "; //sets a value for x
cin>>x;
cout<<"Index: "<<searchArray(a, x)<<endl; //function returns an int therefore a cout is needed
cin >> wait;
return 0;
}

Pass a variable to a function\Return a new value with that variable\Repeat until value reaches 0

I'm having issues understanding how to properly pass a user input value to a function, subtract 1 from that value, return the new value, and place the new value in a loop. So each time the loop executes, it will send the updated value to the function, eventually reaching/stopping at 0. This is what I have so far.
#include "pch.h"
#include <iostream>
using namespace std;
int getBottles(int num);
int main()
{
int num;
int run;
int numBottles;
cout << "Enter the amount of bottle to start with: ";
cin >> num;
if ((num < 0) || (num > 101)) {
cout << "Error! Number isnt valid!\n";
return main();
}
else
{
for (size_t i = 0; i <= run; --i)
{
numBottles = getBottles(num);
cout << "Number of beers on the wall " << numBottles;
}
}
return 0;
}
int getBottles(int num) {
do {
num = num - 1;
} while (num > 0);
return num;
}
int getBottles will subtract 1 from num while num > 0, instead of just once.
Try something like
int getBottles(int num) {
return num-1;
}
And here you're using
for (size_t i = 0; i <= run; --i)
Where run is uninitialized, and you decrement i starting from 0.
Try something like
while (num) {
cout << "Number of beers on the wall " << num;
num = getBottles(num);
}
If you want it to stop before printing out there's no beers on the wall left.

Need to fix a simple loop

Okay I need to write a function that takes an integer parameter and prints the sum of each number up to that point. For example, n = 10 would be 1+2+3+4+5+6+7+8+9+10.
int SumOneToN(int n)
{
int x = 0;
while (x <= n)
{
cout << x+(x+1) << " ";
x++;
}
cout << endl;
}
So what's going on here?
1. Set up the function as SumOneToN.
2. Initialize x to 0.
3. Create a while loop that states while x is less than our parameter, we take x, add it to x+1 (so that we get our current x value added to the next one), print it, then we add to x for the loop to go again until we meet the parameter.
That's how I thought it should work, anyways. What actually returns is:
1 3 5 7.. etc
I'm not sure where I went wrong?
Try this :
int SumOneToN(int n)
{
int x = 1, sum=0;
while (x <= n)
{
sum=sum+x;
cout << sum << " ";
x++;
}
cout << endl;
return sum;
}
Why not use some maths an not have the loop in the first place?
i.e.
int SumToOne(int n) {
return (n * (n + 1))/2;
}
int SumOneToN(int n)
{
int sum=0;
for(int x=1;x<=n;x++)
{
sum+=x;
cout << sum << " ";
}
cout << endl;
return sum;
}
Write the "+" sign in the inverted commas and cout x; once before the while loop. If you want to do it by SUM than you have to introduce another variable and the above solutions are fair enough.
#include <iostream>
using namespace std;
int SumOneToN(int n)
{
int x = 1;
cout << x ;
x++;
while (x <= n)
{
cout << " + " << x ;
x++;
}
cout << endl;
}
int main()
{
int x;
cin >>x;
SumOneToN(x);
return 0;
}
You can try this:
int SumOneToN(int n){
int sum=n,x=1;
while(x<n){
cout<<x<<"+";
sum+=x;
x++;
}
cout<<x;
return sum;
}
Note: This wont print an additional '+' after last number.
Hey you are using same variable for Sum & as looping variable
try this code
int add(int n)
{
int sum=0;
for(int i=1;i<=10;i++)
sum=sum+i;
return sum;
}