Pseudo- andom number generation error [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am the very beginner of the developing field, and I am facing a problem in Pseudo code random error in C++ language.
I think the Error in my program is:
{
srand(time (NULL));
}
Please help me how can I remove this error and the reason of error.
The program I developed is below,
#include<iostream>
using namespace std;
main()
{
int lowerRange;
int upperRange;
lowerRange=1;
upperRange=1;
int secretNumber;
int guess;
guess=10;
cout<<"My Student ID is BC130400789 "<<endl;
cout<<"Enter lower range : ";
cin>>lowerRange;
cout<<"Enter upper range : ";
cin>>upperRange;
cout<<"Computer is calculating a random secret number in the given range...Done!"<<endl;
cout<<"\nPlease guesss the secret number in the range ["<<lowerRange<<" - "<<upperRange<<"]: ";
cin>>guess;
if(guess<10)
{
cout<<"You won! You guess the correct number.. ";
}
else
{
cout<<"Oooppsss...Your entered number is too high...Computer won"<<endl<<endl;
}
{ srand(time (NULL));}
secretNumber = rand()%10+1;
cout<<"Secret number was: "<<secretNumber<<endl<<endl;
}
system("pause");
}

main HAS TO return int
rand() is defined in cstdlib which you don't include
You also need to include ctime for time function
As said by G. Samaras you shouldn't use system("pause") (also you don't include required header for it) and you have mismatched {}.
I would also recommend you reading something about code formatting.

You have an extra bracket at the end. Remove this and the system("pause"); and you will get no errors. Also main() usually returns an int, thus make it int main().
system(“pause”); - Why is it wrong?

Related

Whats Wrong in it? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Problem: https://www.hackerrank.com/challenges/migratory-birds/problem
Test Case Link: https://hr-testcases-us-east-1.s3.amazonaws.com/33294/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1573162301&Signature=MgpSHa3lxX%2FwYwumjzAmF8uviZE%3D&response-content-type=text%2Fplain
for this test case i cant get any output why ?
Thanks in advance
#include<iostream>
using namespace std;
int main()
{
long long n,i=0,num=0,mx=0,r=0;
cin>>n;
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
for(int i=1;i<6;i++)
{
if(arr[i]>mx)
{
mx=arr[i];
r=i;
}
}
cout<<r;
}
This looks problematic:
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
If the input value read into num is greater than 5 (or less than zero), undefined behavior will exist when a value is written into an invalid memory location offset from arr.
There is nothing wrong with you code i got accepted all test cases. But the solution can be return in more optimized way

Unordered_set not working on codechef ide [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
"unordered_set" is not working properly on codechef and giving wrong output on its online ide whereas I am getting right output on geeksforgeeks ide and codeblocks
for Input like 3 2 10 1 100 4 3 i am getting 4 rows as expected in codeblocks and geeksforgeeks because n+m-1 is 4 whereas I am getting only 2 rows in codechef what can be the reason behind and now how it will work on codechef?
#include<stdio.h>
#include<bits/stdc++.h>
#include<unordered_set>
using namespace std;
int main()
{
int n,m,c=0,d,i,j,sum;
int a[10000];
int b[10000];
unordered_set <int> s;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
sum=a[i]+b[j];
if(s.find(sum)==s.end())
{
s.insert(sum);
printf("%d %d\n",i,j);
c++;
}
if(c>=(n+m-1))
{d=1;break;}
}
if(d==1)
break;
}
}
Your program exhibits undefined behavior, by way of accessing the value of an uninitialized variable d.

Calculator Program does not compile [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I wrote a program that implements a simple calculator. However, it does not compile. The compiler says that there are 22 errors, and I don't know why.
Desired behavior:
Asks user about the desired operation
Asks user about the parameters
Outputs result
Specific problem or error:
Compilation errors at any occurrence of cin,cout, endl, case and break
Minimal, Complete, and Verifiable example:
#include <iostream>
int main()
{
float area, r, l, h, b;
int choice;
cout<<"\n area of?";
cout<<"\n[1]square \n[2]rectangle \n[3]circle \n[4]triangle"<<endl;
cin>>choice;
switch(choice);
{
case 1:
cout<<"enter length"<<endl;
cin>>l;
area=l*l;
cout<<area<<endl;
break;
case 2:
cout<<"enter height"<<endl;
cin>>h;
cout<<"enter length"<<endl;
cin>>l;
area=l*h;
cout<<area<<endl;
break;
case 3:
cout<<"enter radius"<<endl;
cin>>r;
area=r*r*3.14;
cout<<area<<endl;
break;
case 4:
cout<<"enter height"<<endl;
cin>>h;
cout<<"enter breadth"<<endl;
cin>>b;
area=h*b*0.5;
cout<<area<<endl;
break;
}
return 0;
}
The are two errors (compile time errors, at least). First of all, cin, cout and endl are not known, you have to write them as std::cin, std::cout and std::endl.
The second problem is here:
switch (choice);
Remove that semicolon and it's fine. The reson why it's not working with the semicolon is because then switch (choice); is its own one and done deal, and the statements after it don't make sense without it.
Also, although it's not causing any compile time errors, I would highly recommend that you indent your code properly. mjcs edited the code you provided for you, it now looks much nicer and it is much easier to find the errors this way. In a big program, it is absolutely vital that the code is indented well, otherwise it's very hard to work with.

What is the difference between the two codes in C++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I was trying the problem Hash Tables: Ice Cream Parlor on Hackerrank. It is a simple question, but here is the bizzare situation i got to. How does the change of data structure matter?
Case 1:
void whatFlavors(vector<int> cost, int money) {
int ans1,ans2;
vector<int> arr(100,0); //notice this
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
And output is:
Case 2:
code:
void whatFlavors(vector<int> cost, int money) {
int arr[100]={0}; //notice this
int ans1,ans2;
for(int i=0;i<cost.size();i++){
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
if(ans1>ans2){
cout<<ans2<<" "<<ans1<<endl;
}else{
cout<<ans2<<" "<<ans1<<endl;
}
break;
}
else{
arr[cost[i]]=i+1;
}
}
}
output:
Let's just notice this part of your code:
if(arr[money-cost[i]]!=0){
ans1=i+1;ans2=arr[abs(money-cost[i])];
This means that your expect money-cost[i] to be negative for some values of i. So you end up reading locations that are outside your array (vector or array) which will lead to undefined behavior in both cases.

simple C++ program to calculate number of instances of a substring [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
can someone point out the error in the following code . I am using a naive approach of comparing both strings character by charcter and updating variable 'u' then comparing it with length of substring . If this is true then variable 'c' is updated by one unit.
Program in C++ :
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
char s[50],a[20];
cin.getline(s,50);
cin.getline(a,20);
//int l=strlen(s);
int p=strlen(a);
int i,c=0,j,u=0,k;
for(i=0;s[i]!='\0';i++)
{
if(a[i]='\0')
{break;}
if(s[i]==a[0])
{
for(j=i,k=0;a[k]!='\0';j++,k++)
{
if(s[j]==a[k])
{
u++;
//continue;
}
//else
//break;
}
//cout<<endl<<u;
if(u==p)
{
c++;
}
}
u=0;
}
cout<<endl<<"count "<<c;
getch();
}
For any kind of input combination , I am getting output as 0.
The problem is with this part:
if (a[i] = '\0')
{
break;
}
First, you are using = instead of ==, but that is not the entire problem. Either change a[i] to s[i], or comment out the entire block. I don't see why it is needed.
My tip and my coding convention to avoid your bug that you use = instead of == in:
if(a[i]='\0')
is to put the rvalue in the left side of the operand and the lvalue in the right side, like this:
if ('\0' == a[i])
this convention will avoid bugs like that(you will get a compilation error):
if ('\0' = a[i])
this code will generate a compilation error:
Error C2106 '=': left operand must be l-value