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 write my code in code blocks and I get this runtime error during execution:
Process returned (0xC0000005)
This is my code
I believe it has something to do with the way I call my functions
main:
int main()
{
int LT[101],i, NI;
float x;
for (i=0 ; i<101 ; i++)
LT[i]=i*i;
NI=find_NI(LT , x);
x=recieve_check_x();
cout<<"square root of x is:"<<(NI+(x-LT[NI])/(2*NI));
return 0;
}
rec_chk.cpp:
#include <iostream>
#include "funcs_hd.h"
using namespace std;
float recieve_check_x ()
{
float x;
cout<<"enter the value of x:"<<endl;
cin>>x;
while (x<0 or x>10000)
{
cout<<"\a error! x must be within range of [0,10000]"<<endl;
cout<<"enter another value:"<<endl;
cin>>x;
}
}
find_NI:
#include <iostream>
#include "funcs_hd.h"
#include <cstdlib>
#include <cmath>
using namespace std;
int find_NI (int LT[] , float x)
{
int i, j , NI;
j=i+1;
double i_delta=abs(LT[i]-x);
double j_delta=abs(LT[j]-x);
if (i_delta<j_delta)
NI=LT[i];
else
NI=LT[j];
return NI;
}
funcs_hd:
#ifndef FUNCS_HD_H_INCLUDED
#define FUNCS_HD_H_INCLUDED
float recieve_check_x (void);
int find_NI (int [] , float);
#endif // FUNCS_HD_H_INCLUDED
I get an error code and the program stops running
int i, j , NI;
j=i+1;
^ indeterminate value
You read an indeterminate value in the program. Therefore the behaviour of the program is undefined.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to binary search more than once using the same function but it's showing segmentation fault. For one input its giving correct answer but when I am giving multiple input for binary search(i.e, q>1 acc. to my code) then its showing segmentation fault.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int x[],int l, int r, long int m ){
if(r>=1){
int mid=(r-1)/2+l;
if (x[mid]==m) return mid;
if (x[mid]>m) return binarySearch(x,l,mid-1,m);
if (x[mid]<m) return binarySearch(x,mid+1,r,m);
}
return 0;
}
int main(){
int n,q;
cin>>n;
int x[n];
for(int i=0;i<n;i++){cin>>x[i];}
int t = sizeof(x) / sizeof(x[0]);
sort(x,x+t);
cin>>q;
while (q--){
long int m;
cin>>m;
cout<<binarySearch(x,0,t-1,m)<<endl;
}
return 0;
}
The condtion r>=1 should be r-l+1>=1 to check if there are some element in the range.
The initialization int mid=(r-1)/2+l; should be int mid=(r-l)/2+l; to correctly calculate the middle element.
Variable-Length Array int x[n]; is not in the standard C++. You should use heap allication int *x = new int[n]; instead. After that, n should be used for the initialization of t because the technique to obtain the number of elements in arrays won't work with pointers.
#include <iostream>
#include <algorithm>
using namespace std;
int binarySearch(int x[],int l, int r, long int m ){
if(r-l+1>=1){
int mid=(r-l)/2+l;
if (x[mid]==m) return mid;
if (x[mid]>m) return binarySearch(x,l,mid-1,m);
if (x[mid]<m) return binarySearch(x,mid+1,r,m);
}
return 0;
}
int main(){
int n,q;
cin>>n;
int *x = new int[n];
for(int i=0;i<n;i++){cin>>x[i];}
int t = n;
sort(x,x+t);
cin>>q;
while (q--){
long int m;
cin>>m;
cout<<binarySearch(x,0,t-1,m)<<endl;
}
delete[] x;
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to sort an array, but somehow the program doesn't output anything?
I tried writing the good old for in for sorting algorithm, doesn't work, then I tried STL, still doesn't work. Here's the code:
#include <iostream>
#include <algorithm>
using namespace std;
void in(int n, int v[]){
cin>>n;
for(int i=1; i<=n; i++){
cin>>v[i];
}
}
void sortf(int &n, int v[]){
sort(v+1, v+n+1);
}
void af(int n, int v[]){
for(int i=1; i<=n; i++){
cout<<v[i]<<" ";
}
}
int main(){
int n, v[1001];
in(n, v);
sortf(n, v);
af(n, v);
}
The variable n in main() is copied to the argument int n of the function in() and changes to the argument inside the function in() will not affect the variable n in main().
Because of that, sortf() and af() are using value of n, which is uninitialized and indeterminate.
To have functions modify caller's variables, you should use references.
#include <iostream>
#include <algorithm>
using namespace std;
void in(int& n, int v[]){ // add & to make n reference
cin>>n;
for(int i=1; i<=n; i++){
cin>>v[i];
}
}
void sortf(int &n, int v[]){
sort(v+1, v+n+1);
}
void af(int n, int v[]){
for(int i=1; i<=n; i++){
cout<<v[i]<<" ";
}
}
int main(){
int n, v[1001];
in(n, v);
sortf(n, v);
af(n, v);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
In the following program:
#include <iostream>
#include <cmath>
using namespace std;
int diagonalDifference(int x[][],int n)
{
int sum1=0,sum2=0,y;
for(int i=0;i<n;i++)
{
sum1+=x[i][i];
sum2+=x[i][n-1-i];
}
y=abs(sum1-sum2);
return y;
}
int main()
{
int n,**z;
cin>>n;
int arr[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
z=diagonalDifference(arr,n);
cout<<x;
return 0;
}
I get a compilation error I don't understand.
error:declaration of 'x' as multidimensional array must have bounds for all dimensions except the first
Could you help me fix it?
int[][] is not a valid type:
int diagonalDifference(int x[][],int n)
You declare z as an int**:
int n,**z;
But you assign it an int:
int diagonalDifference(int x[][],int n);
z=diagonalDifference(arr,n);
And finally you print x which does not exist:
cout<<x;
As rules of thumb:
declare only one variable per line, and give it a meaningful name;
declare what possibly can as const;
Don't use C-style arrays unless you have to; prefer std::vector for instance;
don't use using namespace std;
much more you need to learn.
.
int diagonalDifference(int x**,int n) { /* .... */ }
int matrix_size = 0;
std::cin >> matrix_size;
std::vector<std::vector<int>> matrix{matrix_size, std::vector<int>{matrix_size}};
/* fill the matrix */
const int diag_diff = diagonalDifference(matrix, matrix_size);
std::cout << diag_diff << '\n';
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 6 years ago.
Improve this question
so, I've been trying to solve this problem for a few hours now.
shortly I arrived to a solution that logically - should work and is working, but only for numbers no bigger than 10^7. I guess I could just const the specific number they asked for (600851475143) but I would really love to know - why my code isn't working with big numbers?
this is my code for the solution :
#include <iostream>
#include <stdlib.h>
#include <cmath>
using namespace std;
//enter any number and its largest prime factor will be detected.
int main()
{
long largest(1),num(0);
bool primecheck;
cout<<"enter the desired number :"<<endl;
cin>>num;
if (num%2==0)
largest=2;
cout<<"the relevant factors are: ";
for (int i=3;i<=int((sqrt(num))/2);i+=2)
{
primecheck=true;
for(int j=2;j<i;j++)
{
if(i%j==0)
primecheck=false;
}
if(primecheck)
if(num%i==0)
{
largest=i;
cout<< largest<<"\t";
}
}
cout<<endl<< "the largest prime factor of the number you have entered is: " <<largest;
return 0;
}
thanks in advance! :)
Problem with your code-
Use long long int as data type instead of long int since C++ recognizes long int as having same range as int.
If you want to refer my concise solution in C/C++ -
*For C++ just change the header file to iostream.
#include <cstdio>
#define MAX 775147
#define NUM 600851475143
int main()
{
long long n = NUM;
int max = 3;
for(int i = 3; i <= MAX; i+=2)
{
if(n%i == 0)
max = i;
while(n%i == 0)
{
n /= i;
}
}
printf("%d\n", max);
return 0;
}
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 have to write a program that calculates n times n for the numbers between 1 and 9 using a function.
The output should be like 1, 4, 27, 256...
I can feel I'm very close to finishing it but I just can't figure out what the problem is, here is the code I wrote:
#include <iostream>
using namespace std;
int result, number, n;
void function1()
{
result = number;
for (int x = 1; x < number; x++)
{
result = number*result;
}
}
int main()
{
for (n = 1; n < 10; n++)
{
function1();
cout << result << endl;
system("pause");
return 0;
}
}
Try this :-
#include <iostream>
#include <math>
using namespace std;
int result, number, n;
void function1(int number)
{
int result;
result = pow(number,number);
cout<<result;
}
int main()
{
for (n = 1; n < 10; n++)
{
function1(n);
system("pause");
}
return 0;
}