When I try to compile the below code terminal says stack smashing detected abc2 terminated Aborted (core dumped). This error shows when for loop going through the two dimensional array.
What I want to do is get user input to first column using cin>>arr[0][i]; and 0 for every other columns usingarr[i+1][r]=0;.
#include <iostream>
using namespace std;
void displayArray(int arr[][4],int row,int col);
int main(){
int arr[3][4];
for(int i=0;i<4;i++){
cout<<"enter value ";
cin>>arr[0][i];
for(int r=0;r<3;r++){
arr[i+1][r]=0;
}
}
displayArray(arr,3,4);
return 0;
}
void displayArray(int arr[][4],int row,int col){
for(int i=0;i<row;i++){
for(int r=0;r<col;r++){
cout<<arr[i][r]<<" ";
}cout<<endl;
}
}
You are going beyond the bounds of your array here:
int arr[3][4];
for(int i=0;i<4;i++){
//...
arr[i+1][r]=0; // <-- i+1 when i == 2 is going to give trouble
//...
}
If i >= 2, you're writing to arr[3], arr[4], etc. This is a memory overwrite and the behavior then becomes undefined.
Obviously the fix is either to throttle the loop back so that i is always less than 2, or your array's first dimension needs to be increased from 2 to a bigger number.
Related
I am a beginner and cannot understand why my code is giving segmentation fault error please tell my mistake there are no other errors in the code. the error occurred in vs code in macOS device.the given code is for spirally transversing matrix
#include<iostream>
using namespace std;
# define n 4
#define m 4
void spiral(int r, int c,int arr[n][m])
{
int last_row=r-1 , last_col =c-1;
int left=0, right = last_col;
int top=0, bottom = last_row;
while(top<=bottom && left<=right)
{
for (int i=left;i<=right ;i++){
cout<<arr[top][i]<<" ";
}
top++;
for(int i=top; i<=bottom;i++){
cout<<arr[i][right]<<" ";
}
right--;
if(top<=bottom){
for(int i=right; i>=left;i++){
cout<<arr[i][bottom]<<" ";
}
bottom--;
}
if(left<=right){
for(int i=bottom; i>=top;i++){
cout<<arr[i][left]<<" ";
}
left++;
}
}
}
int main(){
/*int arr[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}*/
int arr[n][m]={
{1,2,3,4},
{1,2,3,4},
{5,6,7,8},
{5,6,7,8},
};
spiral(n,m,arr);
return 0;
}
You just need to look carefully at your code. For instance look at this loop, half way through your function
for(int i=right; i>=left;i++){
cout<<arr[i][bottom]<<" ";
When you get here right equals 2, and left equals 0. So i starts at 2, and keeps increasing, it's always bigger than left, so this loop continues 'for ever'. Very quickly i will be too big for the array you are accessing so you get an access violation.
I guess you meant to write this
for(int i=right; i>=left;i--){
but I'm not sure.
Sometimes all that you need to do is look carefully at your code and see that what you actually wrote is different from what you thought you wrote.
BTW you have the same mistake here
for(int i=bottom; i>=top;i++){
perhaps, again I'm guessing, you meant
for(int i=bottom; i>=top;i--){
I am trying to solve the program of array rotation. I am getting segmentation error in the code. Can someone please tell where is the problem in this code?
this is the question
Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements.
and i have solved it with the following code.
#include <iostream>
using namespace std;
int* rotate(int ar[],int n, int m)
{static int temp[100];
for(int i =0;i<m;i++)
{
temp[i]=ar[i];
}
for(int j =m;j<n;j++)
{
ar[j-m]=ar[j];
}
int x=0;
for(int k =n-m;k<n;k++)
{
ar[k]=temp[x];
x++;
}
return ar;
}
int main() {
//code
int t, n , m;
cin>>t;
while(t>0)
{
cin>>n>>m;
int arr[n];
int * ptr;
for(int i = 0 ;i<n;i++)
{
cin>>arr[i];
}
ptr=rotate(arr,n,m);
for(int j=0;j<n;j++)
cout<<ptr[j]<<" ";
cout<<endl;
t--;
}
return 0;
}
If m > n then it crashes in the first for() loop in rotate as you index past the end of arr.
If m < 0 it crashes in the 2nd loop as it index before arr.
There are probably more cases.
I am trying to input 10 numbers and then call the function(sorting) to sort them in ascending order. After sorting it, the main program will also call the function getAvg to average the numbers in the array. However, after putting 10 values into the array. There are some random numbers displayed before my output.
What am I doing wrong?
#include<iostream>
#include<stdio.h>
using namespace std;
void sorting(int array[]) {
int t;
int x=0;
for (x=0; x<10; x++)
{
for (int y=0; y<9; y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
}
double getAvg(int array[]) {
double sum=0;
double avg=0;
for (int j=0;j<10;j++){
sum=sum+array[j];
}
avg=sum/10;
return avg;
}
int main()
{
int input=0;
int array[10];
double avg=0;
printf("%s","Enter the 10 temperatures \n");
for(int i=0;i<10;i++){
scanf("%i",&input);
array[i]=input;
}
sorting(array);
avg = getAvg(array);
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
printf("%s %.2lf %s","The average is ",avg, ".");
}
I think the error is in this code:
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
Notice that on the first iteration of the loop, you'll have k=0, so this tries to print out array index -1. This results in undefined behavior - technically speaking, anything can happen - and in your case it's reading garbage data from before the start of the array.
To fix this, change the loop bounds so that they properly range over the array:
for (int k=0; k < 10;k++){
cout<<array[k]<<" ";
}
My guess is that you realized that you were reading too far and tried to fix this by subtracting one from the array index, which just introduced a new bug. Hopefully this corrects this!
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
You are showing from -1 to 9, which means 11 values. The 1st value (array[-1]) was not initialized.
You need to change the k<=10 by k<10 and print array[k] instead of array[k-1]:
for (int k=0;k<10;k++){
cout<<array[k]<<" ";
}
The problem is that you are printing the elements at positions -1 through 9 of your array. There is no position -1.
#include <iostream>
using namespace std;
/*
*
*/
int main() {
int k, in[k],reversea[k],i,m,n;
cin>>k;
for (i=0;i<k;i++){
cin>>in[i];
}
for (m=k-1;m>=0;m--){
for (n=0;n<k;n++){
in[m]=reversea[n];
}
}
for(i=0;i<k;i++){
cout<<reversea[i];
}
return 0;
}
I have no idea why it says segmentation fault even before i start debugging it. I compile another one on calculating the frequency of 1, 5, and 10 in an array of k numbers, and it says the same thing...
Here is the other one:
#include <iostream>
using namespace std;
int main() {
int k,i,m,n,count5,count1,count10;
int input[k];
cin>>k;
for (i=0;i<k;i++){
cin>>input[i];
}//input all the numbers
for(i=0;i<k;i++){
if (input[i]=1){
count1++;
}
if (input[i]=5){
count5++;
}
if (input[i]=10){
count10++;
}
}
cout<<count1<<"\n"<<count5<<"\n"<<count10<<"\n";
return 0;
}
Please help me. Thanks.
On this line
int k, in[k],reversea[k]
How are you supposed to initialize an array with k elements if k isn't initialized? The size of an array must be known at compile time not run time. If k isn't know until run time, use a std::vector
int k;
std::cin >> k;
std::vector<int> in(k);
std::vector<int> reversea(k);
Both your programs have two major faults.
You need to know the size of an array while creating it. In your code, k is still uninitialized and you are using this value as the size of your array. Instead, change it to
int k,i,m,n;
cin >> k;
int in[k];
int reversea[k];
While reversing the array, you should be filling reversea using values from in, and not the other way round. Also, you don't need 2 for loops, just use 1 for loop.
for (m=k-1; m>=0; m--){
reversea[m] = in[k-1-m];
}
In the second program, you again need to get the value of k before creating the array input[k].
You are testing for equality with a = instead of == . Change your code from
if (input[i]=1){
to
if (input[i] == 1) {
following code set the value of n and K to zero why this happens??
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{ int n,K;
cin>>n>>K;
cout<<n<<" "<<K<<endl;
int arr[n];
int ranged = (1<<n);
int dp[K][ranged];
for(int i=0;i<n;i++)
{ for(int j=0;j<ranged;j++)
dp[i][j]=0;
}
cout<<n<<" "<<K<<endl;
}
for input
5 3
output:
5 3
0 0
Your code doesn't set the value of n and K to zero, you just set the 2D array dp to 0 inside the for loop. Moreover you create
int dp[K][ranged];
But in for loop you checked for(int i=0;i<n;i++) and assigned dp[i][j]=0; which can buffer overflow.
Change for(int i=0;i<n;i++) to for(int i=0;i<K;i++)
You should most probably get Segmentation Fault. Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.”
Your outer for loop goes from 1 to n but the number of rows you declared in your 2D array was K. So as long as n<=K your code should run fine but if you give n>k ( as 5>3 ) then you should get a segmentation fault ( or undefined behaviour as you are getting!).
Either change
for(int i=0;i<n;i++) to for(int i=0;i<K;i++)
or
int dp[K][ranged]; to int dp[n][ranged];