Dynamic matrix in c/c++ - peculiar error - c++

I was trying to write a small program with a dynamical matrix ( I started it in C, but now I see I need to do it in C++). the main part of it looked like this:
int main()
{
int n,m,i,j,k;
printf("Matrix A n x m:\n");
printf("Input n: ");
scanf("%d",&n);
printf("Input m: ");
scanf("%d",&m);
int** matrix = new int*[m];
if (matrix == NULL) {
printf("no memory\n");
system("pause");
exit(1);
}
for(k=0; k<m; ++k)
{
matrix[k]=new int[n];
if(matrix[k]==NULL)
{
printf("error");
}
}
j=0;
do
{
i=0;
do
{
printf("input (%d,%d):", i,j);
scanf("%d", &matrix[i][j]);
i++;
}
while (i<n);
j++;
}
while (j<m);
}
It compiles with no error, but it doesn't work properly. For example, I can create 2x4 matrix, but no 4x2. After The Input (3,0) message appears, the program crashes. Why?

you mixed between i and j, you should do scanf("%d", &matrix[j][i]);

Your i and j values are the wrong way round. j goes from 0..m-1, i goes from 0..n-1. Your input should be fore &matrix[j][i]. Either that or change the while loops to i < m and j < n.

As I understand it an array must have at least 1 element, if you are compiling in strict ANSI mode.
int m[]; /*ILLEGAL*/
int m[0]; /* definition of 0 size -- ILLEGAL */
please correct me if I am incorrect
You must declare the array element to have (at least) one element if you are compiling in strict ANSI mode
kind regards

Related

How to use scanf in a loop, store the value into one variable and then print it later?

I'm trying to make a program where user is able to input the number of test cases they want, input the number of alphabet, and then print it.
Since I want to do the printf of the Cases after the value of i is the same as input, which means I have to keep the value of word first but the next scanf always overwrite the value of the previous scanf.
Here's my current code:
#include<stdio.h>
int main()
{
int input=0;
int word=0;
int i=0;
int j=1;
scanf("%d", &input); //number of test cases
for(i=0;i<input;i++)
{
scanf("%d", &word); //how many alphabets
}
for(;i>0;i--)
{
printf("Case #%d: ", j);
j++;
if(word==1)
printf("a\n");
if(word==2)
printf("ab\n");
if(word==3)
printf("abc\n");
else
return 0;
return 0;
}
For example, currently the program works like this:
2
1
2
Case #1: ab
Case #2: ab
Which means the second word scanf (2) overwrote its previous value (1).
When I want it to work like this:
2
1
2
Case #1: a
Case #2: ab
I've been searching google for the answer but haven't really found one.
Please tell me how to do it in stdio.h if possible and also what does the function called (as in recursion, selection, etc).
Thank you very much.
Firs of all are you need this code in C or C++? Case you tagged this post as C++ but code is written in C. So I will answer in C.
You have two solutions in this case:
Simple way is to print the Case after user make second input like this:
#include<stdio.h>
int main()
{
int input=0;
int word=0;
int i=0;
scanf("%d", &input); //number of test cases
for(i = 0; i < input; i++)
{
scanf("%d", &word); //how many alphabets
printf("Case #%d: ", i);
if(word==1)
printf("a\n");
if(word==2)
printf("ab\n");
if(word==3)
printf("abc\n");
}
return 0;
}
Or you have to make some dynamic structure to hold all user input and then iterate over it and print your case. In C it will look like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int input=0;
int i=0;
int j=1;
int* word = NULL;
scanf("%d", &input); //number of test cases
if (input > 0) { // check if input cases are more than 0;
word = (int*)malloc(sizeof(int) * input);
}
for(i=0;i<input;i++) {
scanf("%d", &word[i]); //how many alphabets. Write new
}
for(;i > 0;i--) {
printf("Case #%d: ", j);
j++;
if(word[i-1] == 1)
printf("a\n");
if(word[i-1] == 2)
printf("ab\n");
if(word[i-1] == 3)
printf("abc\n");
}
free(word);
return 0;
}
Of corse in case of dynamic array's you need to check if word ptr is not null. But this case shows a bigger picture.
If you decide to use C++ it will be easier because you may use std::vector as dynamic container and there is no need to use pointers.

2D array elements not being read properly

The output is a string of numbers of entire row/column instead of a single number. Can someone please help me in this?
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t;
cin>>t;
while(t--){
int n,m,cnt=0;
cin>>n>>m;
for(int i=0;i<=n+1;i++){
for(int j=0;j<=m+1;j++){
if(i==0||i==n+1||j==m+1||j==0) G[i][j]=0;
cin>>G[i][j];
}
}
cout<<G[1][2]<<endl;//this gives wrong o/p
return 0;
}
Most likely you are reading out of bounds due to a i <= n + 1 and j <= m + 1 conditions in your for loops thus invoking undefined behavior resulting in a corrupted stack which explains the output you are seeing. Modify the boundaries to:
i < n and j < m. Arrays are zero indexed in C++. First array element is accessed via somearray[0] and the last element is somearray[n-1] not somearray[n] which is what you are trying to access in your code. The same goes for multi-dimensional arrays. The statement of:
cout << G[i][j] << endl;
is wrongly placed outside the for loops. It should be inside the inner for loop. The array should be defined as a second statement in your while loop:
int G[n][m]; // Avoid VLAs
That being said variable length arrays are not part of the C++ standard and you should prefer std::vector and std::array to raw arrays.
Assuming G is a 2D array of size n x m, then you go out of bounds here:
for(int i=0;i<=n+1;i++) {
for(int j=0;j<=m+1;j++)
since array indexing starts from 0, and ends at size - 1.
As a result, your code invokes Undefined Behavior. To avoid that, simply change your double for loop to this:
for(int i = 0; i < n; i++) {
for(int j = 0;j < m; j++)

STATUS_STACK_BUFFER_OVERRUN encountered

I have searched for this particular error and found the underlying issue involves loop counts being wrong and causing the program to exceed it's bounds for the array.
However, after I lowered each array to the point where the array began to lose data on output, it continued to throw the same error. I am still new to C/C++ but any insight into this would be greatly appreciated.
The program seems to run through to the very end and even returns to the main method.
#include <stdio.h>
void sortAr(char[]);
int main ()
{
char a='y';
char b,i;
char c[20];
int x=0,n=0,z=0;
while (x<=19)
{
c[x]='#';
x++;
}
printf("Enter 20 letters: \n");
while (z<=20) //(The '=' caused my problem, removed and it runs fine.)
{
z++;
x=0;
b='y';
scanf("%c",&i);
while (x<=19)
{
if (c[x]==i)
b='n';
x++;
}
if (b=='y')
{
c[n]=i;
n++;
}
}
printf("\n");
printf("The nonduplicate values are: \n");
sortAr(c);
}
void sortAr(char ar[])
{
char z;
for (int i = 0; i <= 19; i++)
{
for (int j=i+1; j <= 19; ++j)
{
if (ar[i]>ar[j])
{
z = ar[i];
ar[i] = ar[j];
ar[j] = z;
}
}
}
for (int i = 0; i < 20; i++)
{
if(ar[i]=='#')
continue;
printf("%c ", ar[i]);
}
printf("\n");
}
I found the error at:
while (z<=20)
The reason is the array would overwrite more characters than intended by executing more times than the array had indexed in the memory. As a result it wrote into memory that was not allocated to it and caused the Stack_Buffer_Overrun.
Trace Z:
Z was initialized to 0.
Array was initialized to 20.
While loop starts with Z as the counter for read-ins.
z=0 array=1 1st run,
z=1 array=2 2nd run,
z=2 array=3 3rd run,
z=3 array=4 4th run,
...
z=20 array=21 21st run. (Array cannot hold 21st character and results in Stack_Buffer_Overrun.)
Solution:
change while(z<=20) -> while(z<20)

SIGABRT error in C++

I am getting a SIGABRT error when I compile the following code.(PRIME1 problem of spoj).
Link of the problem is http://www.spoj.com/problems/PRIME1/. It runs well on codeblocks but spoj returns SIGABRT error. Can someone explain the reason?
int main()
{
long long k,x,j=0,size,l=0,p=0,q=0,r=0,s;
cin>>size;
int a[(2*size)];
cout<<endl;
for(int i=0; i< (2*size); i++)
{
cin>>a[i];
}
if( size == 1)
{
p=a[1];
}
else
{
do
{
if(a[l+3]>a[l+1])
{
p=a[l+3];
}
else
{
p=a[l+1];
}
l=l+2;
}while(l<2*(size-1));
}
cout<<p;
long * b = new long [p-1];
for(long long i=0;i<p-1;i++)
{
b[i]=1;
}
b[0]=b[1]=0;
s=sqrt(p)
for(long long i = 2; i <= s; i++)
{
if(b[i] == 1)
{
for(long long j = i*i; j <= p; j = j + i)
{
b[j] = 0;
}
}
}
while(r<(2*size))
{
for(long long i = a[r];i < a[r+1];i++)
{
if(b[i] == 1 )
{
cout << i << "\n";
}
}
cout<<endl;
r=r+2;
}
delete [] b;
}
You are accessing array element accessing outside bounds
Array size 2*size-1 So elements from 0 to 2*size-2
But in your for loop you are going upto 2*size thus accessing 2*size-1 which is outside bounds
int a[(2*size)-1];
This is not legal C++ code (it's using a GCC extension), but it obviously compiled, so we'll let that slide. You are accessing your array out of bounds in the following loop and all over the place later on, which is undefined behavior - you need an array of size 2 * size to read in all the supplied parameters. Although given that they guarantee that size <= 10, you might as well just declare it as int a[20];
But that probably didn't cause the crash. What caused the crash is probably this line:
long * b = new long [p-1];
What's p? Well, let's just consider the easy case of size = 1 where you set p to a[1], or the second number you read in. What's the bounds on that number?
The questions says that the bound is n <= 1000000000, or 109. Your new can be requesting as much as 8GB of memory, depending on the value of sizeof(long) in the system you are using. The allocation is almost certainly going to fail, throwing a std::bad_alloc exception that causes std::abort() to be called as you don't have any exception handling code.
You initialize a to 2 * size - 1 elements...
int a[(2*size)-1];
Yet you write 2 * size elements.
for(int i=0; i< (2*size); i++)
// ...
Your loop should be:
for(int i=0; i< (2*size-1); i++)
Next...
if(size == 1)
{
p=a[1];
}
If size == 1 then you allocated an array of 2 * 1 - 1 = 1 element, so a[1] is an invalid access (you only have a[0] as arrays are 0-indexed).
You then have stuff like this:
if(a[l+3]>a[l+1])
Which loops until l == 2*size-1, so l+3 is invalid as soon as you hit 2 * size - 1 - 3.
Basically you just have a lot of places where you're reading or writing past the end of an array or not ensuring proper initialization and invoking undefined behavior.

edit a range of elements inside a 2d array?

well ive been trying to edit the element of an array so lets assume that we have a 2d array
so
a 2d array 9 x 9;
for(... ... ... ++)
{
for(.. ... ...++){}
}
lets say that the code will use another set of for loops to display the 2d array its a simple array nothing fancy
00000000
00000000
00000000...
so if i wanted to to display an E from elements[1][0] to [2][3] how would i do that?
00000000
eeeeeeee
eeee0000
00000000
what i had in mind was something like while(x < y)
{ array[x++][y];}
but this idea doesnt seem to work.
would gladly take any help. thx
for(int i=0; i<9; i++) //This loops on the rows.
{
for(int j=0; j<9; j++) //This loops on the columns
{
board[i][j] = grid; // set the array to the char on grid '0'.
}
}
board[s_col][s_row] = 'Z';
while(s_col < e_col)//s_col is the start of the rows and columns
//{
//if(s_col != e_col)
{
++s_col;
board[s_col][s_row];
}
//}
//cout << board[s_col][s_row++] <<endl;
// display the array
for(int i=0; i<9; i++) //This loops on the rows.
{
for(int j=0; j<9; j++) //This loops on the columns
{
cout << board[i][j] << " ";
}
cout << endl;
}
You were on the right track with the approach:
for(... ... ... ++)
{
for(.. ... ...++){}
}
Here is some code that should help you:
#include <stdio.h>
#include <memory.h>
#define MAX_ROW 4
#define MAX_COL 8
void fillRange(char fillChar, int startRow, int startCol, int count);
char myArray[MAX_ROW][MAX_COL];
void printArray();
int main(int argc, char *argv[])
{
memset(myArray, '0', sizeof(myArray));
printf("\nBefore:\n");
printArray();
fillRange('e', 1, 0, 12);
printf("\nAfter:\n");
printArray();
}
void fillRange(char fillChar, int startRow, int startCol, int count)
{
int i, j, filledChars = 0;
for(i = startRow; i < MAX_ROW; i++)
{
for(j = startCol; j < MAX_COL; j++)
{
myArray[i][j] = fillChar;
if(++filledChars == count)
return;
}
}
}
void printArray()
{
int i, j;
for(i = 0; i < MAX_ROW; i++)
{
for(j = 0; j < MAX_COL; j++)
putchar(myArray[i][j]);
printf("\n");
}
}
If you instead wanted to end at a particular point in the array then you just need to change the condition that triggers the return.
This is one of the many reasons why the more coding you do the more you tend to avoid 2D arrays of the sort you have.
Instead you use a 1D array like this: board[i][j] == board_1D[i*num_columns+j]. That also means you can just iterate through the entire board in a single for loop.
Now you simply calculate the begin and end indices of your e range, and simply test if your counter is within that range. In other words, you have a single if statement inside your inner-most loop.
You can, of course, convert your i,j 2D indices into the equivalent 1D index and take the same approach. index_1D = i*9+j;
I'll leave the code to you.
Dealing with a sequence of adjacent values is easiest done when you have an underlying contiguous array and you don't need to deal with double indexing (see Adam's answer on that). However, in your simple case it also quite doable:
You'd initialize your row and column variables with the start row and column (in your case 0 and 1). You then walk with your column until you reached either the target column (2) and you are on the target row (3) or you reached the end of the matrix in which case you set the column to 0 and increment the row.