Hi. I'm new to C++, but I have written a similar program in Python. Every time I run the program with the same inputs, it generates different results, despite there being no random element involved.
#include <iostream>
#include <cmath>
usingnamespace std;
int x,y,a,b;
int *c=newint [x];
float z;
int main()
{
cout<<"Please enter the radicand: ";
cin>>x;
cout<<"\nPlease enter the index: ";
cin>>y;
for (a=1; a<x; a++);{
if ((x/a)==ceil(x/a));{
c[a-1]=(x/a);
}
}
cout<<c[0];
}
I am currently using the Code Blocks IDE version 10.05.
Edit: This is going to be part of a program to simplify radicals.
You shouldn't declare variables as global unless you absolutely have to. Your best bet is to declare a variable right before you use it. In fact, in the case of 'a', you can actually declare it inside the for statement:
for(int a = 0;;)
You're initializing c to store x amount of ints, but you haven't even initialized x yet. This is probably the cause of your "randomness". The program needs space for x, so it gets some, but it doesn't actually change what's already in that space until you do so manually. So x will usually be different on different runs.
The number isn't random, it's the first number entered.
I don't understand why you have used a pointer so it's hard to understand what you were trying to do, and so I don't know how to help (by the way, don't forget to use the `delete` if you use `new`. your program creates a memory leak).
As for a solution:
Could you please explain what the purpose of the program? what is it meant to do?
Side note: you should remove the semi-column (`;`) after the `if`, and `for` statement.