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.
Related
I'm just starting so I'm trying to write a program which determine if a number is positive or negative.
#include <iostream>;
int step_function(int x) {
int result = 0;
if (x > 0)
result = 1;
else if (x < 0)
result = -1;
else
result = 0;
return result;
}
using namespace std;
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
int a = step_function(num);
if (a == 1)
printf("%d is positive", num);
else if (a == -1)
printf("%d is negative", num);
else
printf(" it is zero");
return 0;
}
There is a few things you should do:
First things first you should get yourself a Good Book for C++.
Second thing is read why using namespace std; is a bad idea.
Lastly here is your code fixed. You needed to remove the semicolon as well as removing the printf(). I also removed the using namespace std; which made it more readable.
#include <iostream>
int step_function(int); //Function prototype
int main() {
int num;
std::cout << "please enter number : ";
std::cin >> num;
int a = step_function(num);
if (a == 1)
std::cout << num << " is postive";
else if (a == -1)
std::cout << num << " is negative";
else std::cout <<" it is zero";
return 0;
}
int step_function(int x)
{
int result = 0;
if (x > 0) result = 1;
else if (x < 0) result = -1;
else result = 0;
return result;
}
Don't use semicolon after #include <iostream>.
I think for C++ cout is more standard whereas printf is from C.
You can also include printing of the text in the step_function. Also, it's better to use braces {} after if and else statements for clarity especially if the code becomes complex.
#include <iostream>
using namespace std;
void step_function(int x) {
if (x > 0) {
cout << x << " is positive" << endl;
}
else if (x < 0) {
cout << x << " is negative" << endl;
}
else {
cout << "it is zero" << endl;
}
}
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
step_function(num);
return 0;
}
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.
#include "stdafx.h"
#include "math.h"
#include <string>
#include <iostream>
using namespace std;
int main ()
{
int x;
cout << "Enter a number." << endl;
cin >> x;
int y = 1;
int i = 0;
while (i == 0 && y < sqrtf(x))
{
if (fmodf(x,y) == 0)
{
i = 1;
}
else
{
i = 0;
}
y++;
if (i == 1)
{
cout << "Your number is prime." << endl;
}
else
{
cout << "Your number is composite." << endl;
}
}
return 0;
}
This is a code I created to test for primes. After sorting out several debugging issues I was able to run it.
It opened the command window, read 'Enter a number' and closed itself the second I entered a number.
Help?
You have to:
close the while loop in the correct place
change the if (i == 1) condition (i==1 means x is divisible by some y)
start with y = 2 (every number is divisible by one)
include sqrtf(x) in the loop (y <= sqrtf(x) or 15, 25, 35... are primes).
So:
int main()
{
int x;
cout << "Enter a number." << endl;
cin >> x;
int y = 2; // <-- changed
int i = 0;
while (i == 0 && y <= sqrtf(x)) // <-- changed
{
if (fmodf(x,y) == 0)
{
i = 1;
}
else
{
i = 0;
}
y++;
} // <-- moved here
if (i == 0) // <-- changed
{
cout << "Your number is prime." << endl;
}
else
{
cout << "Your number is composite." << endl;
}
return 0;
}
works (more or less...).
Anyway:
don't use using namespace std; (Why is "using namespace std" considered bad practice?)
\n should be your default ("\n" or '\n' or std::endl to std::cout?)
y and x are integers so you can use % instead of fmodf
avoid premature pessimization: prefer preincrement, only use postincrement if you're going to use the original value
else { i = 0; } is superfluous
you can change y < sqrtf(x) with y * y <= x (and you don't need math.h anymore) or find square root of number then start the loop
Somewhat better (but far from perfect):
#include <cmath>
#include <iostream>
int main()
{
int x;
std::cout << "Enter a number.\n";
std::cin >> x;
int square_root = std::sqrt(x);
int y = 2;
int i = 0;
while (i == 0 && y <= square_root)
{
if (x % y == 0)
i = 1;
++y;
}
if (i == 0)
std::cout << "Your number is prime.\n";
else
std::cout << "Your number is composite.\n";
return 0;
}
Now:
input validation, i.e. check for bad input values (How to check if input is numeric in C++)
special cases checking (is the number one a prime number?)
a bool would express your intentions better than int i;
improve the algorithm (Determining if a number is prime, Which is the fastest algorithm to find prime numbers?, Primality tests)
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;
}
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;
}