while infinite loop? - c++

I came across this question in this forum
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int x=0;
while (x<3) {
x = x++;
cout << x << endl;
}
return 0;
}
given the code above, why is the while loop infinite? Using gcc 4.4 under mac os, the while loop does terminate :) so the question does not apply for all architectures. The output I get tough is
1
2
3
I don't see 0, and I guess the reason is related to the double assignment?

x = x++;
is undefined behavior

you never see zero because the increment is before the cout.

Related

Code is returning zero from a mathematical expression [duplicate]

This question already has answers here:
Why is my double or int value is always 0 after division?
(5 answers)
Closed last year.
#include <iostream>
#include <cmath>
using namespace std;
int Factorial_of(int n)
{
int fact = 1, i;
for(i=1; i<=n; i++)
fact = fact * i;
return fact;
}
int main()
{
cout << Factorial_of(4) / (Factorial_of(10) * Factorial_of(abs(4-10))) << endl;
}
Does anyone know why this is printing 0?
Update:
After changing the code based on #M. Twarog's answer, it is outputting "9.18577e-009" now, Does anyone know why?
#include <iostream>
#include <cmath>
using namespace std;
double Factorial_of(int n)
{
double fact = 1;
int i;
for(i=1; i<=n; i++)
fact = fact * i;
return fact;
}
int main()
{
double res=Factorial_of(4) / (Factorial_of(10) * Factorial_of(abs(4-10)));
cout<<res;
}
There are some errors/mistakes . First for division always go with double variable. And Your code is returning value in point so return type should be double. Also you miss << in cout statement before endl.
I can explain the output you get after fixing things: its the right answer

Do not know how to implement a thing in a homework task

Now, the question is :
Assemble a program that will simulate the work of automatic telephone exchanges. For example, there are 10 subscribers, anyone can call anyone, Each has several conditions:
waiting for an answer, calls, says, free. They randomly call each other, the program should show the operation of this system.
And, I figured out how to do some of it, but, don't know how to exactly implement it.
#include <cstdlib>
#include <chrono>
#include <random>
#include <string>
using namespace std;
int i, a;
string state[4]{ "free", "waiting", "calling", "talking" };
int states[4]{ 1,2,3,4 };
int subs[10]{ 1,2,3,4,5,6,7,8,9,10 };
int main(int subs[10], int states[4], string state[4])
{
srand(time(nullptr));
for (int x = 0; x < subs[10]; x++)
{
states[i] = rand() % 4;
states[i] = a;
cout << "Subscriber" << subs[x] << "is" << state[a] << endl << endl;
}
}
Right here, I also have an error in line states[i] = a
Now, what I tried to do there was to randomize a number, and then let it get assigned to any subscriber, and then showed to the person who runs the program. But, well... This is not exactly what the question told me to do. And, I am not sure what I can do, here. I also have limited time for this, with only 12 hours left to do this, because I am a lazy bum. Help please?
Clear version of your code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(void) {
int subs[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
size_t len = sizeof(subs) / sizeof(subs[0]);
string subsStatus[10];
string state[] = {"CALLING", "WAITING", "FREE", "TALKING"};
srand(time(NULL));
for (int i = 0; i < len; i++) {
subsStatus[i] = state[rand() % 4]; // example: Subscriber4 = "TALKING"[3]
cout << "Subscriber" << subs[i] << " is " << subsStatus[i] << endl;
}
return 0;
}
There are some guidelines as side tip:
You don't need to use nullptr in srand(time())
If I'm right, then the function main() is only supposed to accept int argc, char *argv[] and few more arguments, don't define your owns since you're not asking before launch of program from terminal.
states[i] = a defines states[i] to a not vice versa.
You don't need to define array length in case you're defining via coding, i.e. int arr[] = {...}, it'll be expanded automatically, you must use c++11 and above for this feature.

Windows compilation error when running a small program

When I ran my solution to The Square Within from codercharts.com on my computer (Intel i5 newest version), I was able to build my program in 646 ms, but for some random reason I was able to enter the code for the input part of my code, but when I pressed "enter" to get results, the screen popped up with a Windows error:
Here is my code that I ran (logic all follows the problem I was given).
#include <iostream>
using namespace std;
int main (int argc, char ** argv)
{
int dimension[]={};
int result;
int counter = 0;
for (int i=0; i < 1000000; i++){
counter+=1;
}
for (int a=0; a<counter; a++){
result=(dimension[a]*(dimension[a] + 1)*((2*dimension[a]) + 1))/6;
}
while (true){
cin >> dimension[counter];
break;
cout << result << " ";
}
return 0;
}
Can something review my code and help me with this hard problem? Thanks in advance!
The line
int dimension[]={};
needs a value for its size:
i.e.
int dimension[1000001]={};

How to Count decimal points using for loop?

It's my first time to deal with Decimal points in C++ and i was wondering if i want the for loop to increase 0.01 by 0.01 instead of 1.0 by 1.0 what can i do.
I tried this but obviously it didn't work.
#include <iostream>
using namespace std;
int main(){
double i;
for (i=1.5;i<1.68;i++);
cout<<i;
system("pause");
return 0;
}
How can i get this done?
Thank you in advance.
for (i = 1.5;i < 1.68;i = i + 0.1)
cout << i << endl;
there will be no ; after for loop otherwise it will become a statement and not work correctly..
floating points nature is unexpected so it sometimes becomes headache because of precision loss..so try to avoid them using in loop..always try to use integer in a loop..
so here is another answer and it is better than above and is less prone to any unexpected results
int i;
for (i = 150;i < 168;i += 10) {
cout << i / 100.0 << endl;
}
Try this:
#include <iostream>
using namespace std;
int main(){
int i;
for (i=150;i<168;i+=10)
{
double d=i/100.0;
cout<<d;
}
system("pause");
return 0;
}
One of mistakes in your code for (i=1.5;i<1.68;i++); is ";" after ")". It means, that this loop does nothing. I.e. next line cout<<i; is not inside body of loop.

c++ simple pointer program [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 9 years ago.
Improve this question
Here is a simple program where it sums all of the numbers sent in on the command line. It should take an arbitrary number of values.
It keeps giving me a 0 for each line. I've tired to make several changes but it continues to give the same output
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; i++)
{
int sum=0;
sum+=atoi("argc[i]");
cout << sum << endl;
}
return 0;
}
"argc[i]" is a literal string so when converted by atoi gives 0! sum should be initialized before the loop :
int main(int argc, char *argv[]) {
int sum=0;
for (int i = 1; i < argc; i++) {
sum += atoi(argv[i]);
}
cout << sum << endl;
return 0;
}
Conventionally argv[0] is the name of the program (or at least the name used in the command line to invoke the program), so better start at index 1.
You need to spend dozen of hours reading more your books and experimenting on your computer. Asking here such a basic question don't help you at all (and is considered as rude...).
Don't forget to enable debugging info and all warnings when compiling (e.g. with g++ -Wall -g if using GCC). Then, learn how to use the debugger (e.g. gdb).
Your basics are not clear, I suggest you to read the book.
Program should be like this:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
int sum=0;
for (int i = 0; i < argc; i++)
{
sum+=atoi(argv[i]);
}
cout << sum << endl;
return 0;
}
move parameter sum outside loop!!
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
int sum=0;
for (int i = 0; i < argc; i++)
{
sum+=atoi(argv[i]);
cout << sum << endl;
}
return 0;
}