For some reason, the first output my program is giving, is garbage value, while the second output is correct.
This is a problem from HackerRank.
I know this question has already been asked by someone else. I just want to know what the problem is in my code.
#include <stdio.h>
int main()
{
int index,query;
int count1 = 0;
scanf("%d%d",&index,&query);
for(int i=0;i<index;i++)
{
int b;
scanf("%d",&b);
int A[index][b];
for(int j=0;j<b;j++)
{
scanf("%d",&A[i][j]);
}
count1++;
if(count1<index)
{
continue;
}
int count2=0;
while(count2<query)
{
int d,e;
scanf("%d%d",&d,&e);
printf("%d\n",A[d][e]);
count2++;
}
}
return 0;
}
If the input is:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Then the output should be:
5
9
But instead, my output is:
garbage
9
Disclaimer
I didn't even click the link, so I do not know if the solution is correct, but assuming that you got the logic right..
The problem
is that you populate in stages a local to the body of the for loop 2D array, which at the end of your processing, you expect to have it accessible (I mean the complete matrix, populated from every single iteration of the for loop).
Instead, you get only the last's iteration declared array, that's why you get only the A[1][3] element right, and not the A[0][1], since the 2nd row is populated in the last (2nd iteration), while the 1st row is populated in the first iteration (of the firstly declared A), which goes out of scope as soon as the first iteration terminates.
The fix
So, what you need to fix this is to dynamically allocate memory for your matrix, and every time a new dimension for the columns is inputed, resize it with realloc().
I believe that the explanation I have in 2D dynamic array (C) will help you, since what you want is the number of rows fixed, and the number of columns adjustable on every iteration.
Below is an illustration based on the link I shared above, which visualizes what exactly is your matrix (a 1D array of pointers), and shows how the code below manipulates it:
Full code example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int index,query;
int count1 = 0;
scanf("%d%d",&index,&query);
// An array of `index` (e.g. 2) pointers to integers
int *A[index];
// Initialize all the pointers to NULL
for(int k = 0; k < index; ++k)
A[k] = NULL;
for(int i=0;i<index;i++)
{
int b;
scanf("%d",&b);
// Replaced your 'int A[index][b];' with the following:
// Every time a new number of columns (that's 'b') is read,
// we need to adjust the numbers of columns our matrix ('A') has.
// That means, that for every pointer (row), we need to re-allocate
// the number of columns it points to, which is basically a 1D array, of dimension 'b'
for(int k = 0; k < index; ++k)
A[k] = realloc(A[k], b * sizeof(int) );
for(int j=0;j<b;j++)
{
scanf("%d",&A[i][j]);
}
count1 ++;
if(count1<index)
{
continue;
}
int count2=0;
while(count2<query)
{
int d,e;
scanf("%d%d",&d,&e);
printf("%d\n",A[d][e]);
count2++;
}
}
// Free the dynamically allocated memory
for(int k = 0; k < index; ++k)
free(A[k]);
return 0;
}
Output (for the input provided):
5
9
Pro-tip: The typical methodology of calling realloc() is to use a specific pointer for the reallocation, test that pointer and, if everything worked out ok, change the old pointer, as explained in Does realloc overwrite old contents?, which I didn't do in that post for the sake of being "laconic".
The C VLA is not suitable here. It seems you need to allocate memory dynamically. The only VLA that can be used is an array of pointers to other arrays. All other arrays should be allocated dynamically.
Something like the following.
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
size_t number_of_arrays;
size_t number_of_queries;
scanf( "%zu%zu", &number_of_arrays, &number_of_queries );
int **a = malloc( number_of_arrays * sizeof( int * ) );
for ( size_t i = 0; i < number_of_arrays; i++ )
{
size_t size;
scanf( "%zu", &size );
a[i] = malloc( size * sizeof( int ) );
for ( size_t j = 0; j < size; j++ ) scanf( "%d", &a[i][j] );
}
for ( size_t i = 0; i < number_of_queries; i++ )
{
size_t array_index;
size_t element_index;
scanf( "%zu%zu", &array_index, &element_index );
printf( "%d\n", a[array_index][element_index] );
}
for ( size_t i = 0; i < number_of_arrays; i++ ) free( a[i] );
free( a );
}
If to input
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
then the program output will be
5
9
As for your code then it is invalid. For example the variable b is not initialized so the declaration of the array has undefined behavior.
int b;
scanf("%d",&b);
int A[index][b];
^^^
Hint : Variable sized arrays need to be dynamically allocated, here's how to it in C
int rows;
scanf("%d",&rows);
//2D array
int**A = malloc(sizeof(int*)*rows); //allocate number of rows
//for each row allocate number of colums
for(int i = 0; i < rows; i++)
{
int cols;
scanf("%d",&cols);
A[i] = malloc(sizeof(int)*cols);
}
Related
Hello everyone,
I wrote an code which is remove duplicates from sorted array. I know that there are lots of people have a good know how about clean code in this platform. I want to write code especially clean code. So I wanna hear some suggestion this about. If yo critise my code I am glad.
#include <stdio.h>
int main()
{
int arr[]={1,2,2,2,3,4,4,4,5,6,7,7,7,8,8};
int arr_size = *(&arr + 1) - arr;
printf("length of array %d\n",arr_size);
int result[arr_size];
int last_item= result[0];
for (int i=0; i<arr_size;i++)
{
if(arr[i]!=last_item)
{
last_item= arr[i];
result[i]= last_item;
printf("result : %d\n", result[i]);
}
}
return 1;
}
For starters it is unclear why main returns 1 instead of 0.
Secondly you are not removing duplicate elements from an array. You are trying to copy unique elements from one array into another array.
Also it is unclear why the result array has arr_size elements as the source array.
This declaration
int last_item= result[0];
is invalid because the result array is not initialized. So it contains indeterminate values.
Also in the for loop you are using the invalid index i for the result array. So again some elements of the result array will have indeterminate values.
result[i]= last_item;
And within the for loop there should not be any call of printf. If you want to output the result array (it is already another task) then you need to use a separate loop or function.
And at last you should write a separate function that performs the task of copying unique or removing duplicate elements..
I would write a function that indeed removes duplicates from an array the following way as shown in the demonstration program below.
#include <stdio.h>
size_t remove_duplicates( int a[], size_t n )
{
size_t m = 0;
for (size_t i = 0; i < n; i++)
{
if ( i == 0 || a[i] != a[m-1])
{
if (i != m)
{
a[m] = a[i];
}
++m;
}
}
return m;
}
int main( void )
{
int arr[] = { 1,2,2,2,3,4,4,4,5,6,7,7,7,8,8 };
const size_t N = sizeof( arr ) / sizeof( *arr );
for (size_t i = 0; i < N; i++)
{
printf( "%d ", arr[i] );
}
putchar( '\n' );
size_t m = remove_duplicates( arr, N );
for (size_t i = 0; i < m; i++)
{
printf( "%d ", arr[i] );
}
putchar( '\n' );
}
The program output is
1 2 2 2 3 4 4 4 5 6 7 7 7 8 8
1 2 3 4 5 6 7 8
I have an array of data, arr, and an array of indexes, index. My goal is to use a for loop to create new arrays of data partitioned at each of the indexes and to further find the minimum in each partitioned array. I am using malloc to create a dynamic array which I then free at the end of each loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int j;
int arr[] = {1,3,4,6,7,8,12,87,89,12,34,43,54,67,81,2,0,10,23,45,81,23,89,23,56,81,28,79};
int index[] = {1,5,9,13,19,24};
int h = 27;
int k;
int c;
for(j = 0;j < h - 1;++j)
{
int *temp_arr = malloc(10*sizeof(int));
for(k = index[j];k<(index[j+1]);++k )
{
temp_arr[k] = arr[k];
}
int local_min ;
local_min = temp_arr[0];
for ( c = 1 ; c < sizeof(temp_arr) / sizeof(temp_arr[0]) ; c++ )
{ printf("Temp array %d ", temp_arr[c]);
if ( temp_arr[c] < local_min)
{
local_min = temp_arr[c];
printf("Local min in loop %d ", local_min );
}
}
free(temp_arr);
printf("\n");
}
return 0;
}
Unfortunately, the program is crashing without giving me any error messages. I think that I may have done something fundamentally wrong using malloc. Any suggestions on how to correctly do this would be greatly appreciated.
for(j = 0;j < h - 1;++j) // Where h = 27
It means j can be max 25
for(k = index[j];k<(index[j+1]);++k ) // If j = 25
You are touching index[26], while index has 6 elements.
for ( c = 1 ; c < sizeof(temp_arr) / sizeof(temp_arr[0]) ; c++ ) // Where int *temp_arr
temp_arr is pointer and sizeof(pointer) is always 8 on 64bit or 4 on 32bit system.
for(k = index[j];k<(index[j+1]);++k )
{
temp_arr[k] = arr[k]; // Where int index[] = {1,5,9,13,19,24};
}
If k is 13 or 19 or 24 you are ouside of bounds.
You should take a look at valgrind, and debug your code, step by step, expression by expression.
It appears that you forgot to inspect the output from Valgrind, which shows exactly where you use uninitialized values and where you run off the end of temp_arr.
Note also that temp_arr is a pointer type, and sizeof temp_arr is the size of that pointer, not the size of the array it points to.
I am successfully storing the calculated subsets in a 2-D array matrix in C language.Now I want to print the subsets in an order desired.
For eg.
2-D array matrix is
10 7 3 2 1
10 7 5 1
7 6 5 3 2
10 6 5 2
10 7 6
Desired Output
10 7 6
10 7 5 1
10 7 3 2 1
10 6 5 2
7 6 5 3 2
How quick sort can be applied to sort/order these rows?
As #chqrlie noted, this can be easily solved with qsort.
Depending on the way the matrix is declared (is it an array of pointers to arrays of ints? do all arrays have the same length? is it a global array of fixed size?) the code will have to do slightly different things.
So, assuming the array is a global variable and all rows have same length (padded with 0s):
MWE:
#include <stdio.h>
#include <stdlib.h>
/*
Compare 2 integers
returns:
-1 if *i1 < *i2
+1 if *i1 > *i2
0 if *i1 == *i2
*/
int intcmp(const int *i1, const int *i2)
{
return (*i2 < *i1) - (*i1 < *i2);
}
#define ROWS 5
#define COLS 5
/*
Assumes rows already sorted in descending order
NOTE: qsort calls the comparison function with pointers to elements
so this function has to be tweaked in case the matrix is an array of
pointers. In that case the function's declaration would be:
int rowcmp(int **pr1, int **pr2)
{
const int *r1 = *pr1;
const int *r2 = *pr2;
// the rest is the same
}
*/
int rowcmp(const int *r1, const int *r2)
{
int i = 0, cmp;
do {
cmp = intcmp(&r1[i], &r2[i]);
i++;
} while (i < COLS && cmp == 0);
return -cmp; /* return -cmp to sort in descending order */
}
int data[5][5] = {
{10,7,3,2,1},
{10,7,5,1,0},
{ 7,6,5,3,2},
{10,6,5,2,0},
{10,7,6,0,0}
};
void printmatrix()
{
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d ", data[i][j]); /* leaves a trailing space in each row */
}
printf("\n");
}
}
int main()
{
printmatrix();
qsort(data, 5, sizeof(data[0]), (int (*)(const void *, const void *))rowcmp);
printf("\n");
printmatrix();
return 0;
}
For the most flexible solution, I would define
struct row {
size_t len;
int *elems;
};
struct matrix {
struct row *rows;
size_t nrows;
};
and change the code accordingly.
NOTE: code not thoroughly tested, use with caution ;)
First of all, are you sure that the 1 on row 3,col 5 should be there and not on the last line?
Anyway, an efficient way to achieve what you want is:
compute the frequency array
declare a new matrix
go from the highest element (10 in your case) from frequency array and put in your matrix using your desired format.
It is time-efficient because you don't use any sorting algorithm, thus you don't waste time there.
It is NOT space-efficient because you use 2 matrices and 1 array, instead of only 1 matrix as suggested in other posts, but this should not be a problem, unless you use matrices of millions of rows and columns
C code for frequency array:
int freq[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i=0; i<NO_ROWS; i++) {
for(int j=0; j<NO_COLS; j++) {
if(MATRIX[i][j]!=null && MATRIX[i][j]>0 && MATRIX[i][j]<11) {
freq[MATRIX[i][j]]++;
}
}
}
C code for computing the new matrix dimensions
(assuming you want to keep the number of rows)
OUTPUT_MATRIX[100][100] /*I declared it statically, but I would advise to make it dinamically */
/* first, compute the number columns.
To do so, we need the number of elements
(we get them by simply summing up frequency array's elements) */
int s=0;
for(int i=0; i<11; i++) {
s+=frequency[i];
}
int addOne = 0 /* boolean value to check if we will have to add one extra column for safety */
if(s % NO_ROWS) {
addOne = 1; /* division is not even, so we will have to add extra column */
}
NO_COLS = s/NO_ROWS + addOne;
Now, final part, assigning the values from frequency array to the OUTPUT_MATRIX
int k=0;
int currentNumber = 10; /* assigning starts from 10 */
for(int i=0; i<NO_ROWS; i++) {
for(int j=0; j<NO_COLS; j++) {
if(currentNumber>0) {
if(frequency[currentNumber]==0 || k>=frequency[currentNumber]) {
currentNumber--;
k=0;
}
OUTPUT_MATRIX[i][j] = frequency[currentNumber];
k++;
} else {/*here, you can assign the rest of the value with whatever you want
I will just put 0's */
OUTPUTMATRIX[i][j] = 0;
}
}
}
Hope this helps!
This is what I do in C++ to reorder a matrix:
// b is the matrix and p is an array of integer containing the desired order of rows
for(i=0; i<n; i++){
if( p[i]==i )
continue;
b[i].swap(b[p[i]]);
j = p[i]; // New row i position
// Update row i position to new one
for(int k=i+1; k<n; k++){
if( p[k] == i )
p[k] = j;
}
printRow( b[i] );
}
You need to define an array of pointers of the data type you use and then you can reorder your matrix.
for example your matrix is: arr[5][10], and you want to print line 4 before line 3:
int *[5] arr2;
arr2[0] = &arr[0][0];
arr2[1] = &arr[1][0];
arr2[2] = &arr[2][0];
arr2[3] = &arr[4][0];
arr2[4] = &arr[3][0];
in regard to how will the ordering algorithm work, i would suggest placing a header in the start of each array in the matrix which will tell you how many elements it has(basically the first element of each array can be a counter of the total elements) afterwards you can order the strings by comparing the header, and if it is equal comparing the first element and so on. this can be done in a loop that iterates as many times as there are elements in the array, when the elements are not equal, break out of the loop.
hope this helps.
Well I have been studying a little C this winter break and in my adventures I stumbled upon an issue with a Dynamic Array.
It's a fairly simple program really. What I am trying to do is to create an array that holds the numbers of the Fibonacci series. Here is the code:
#include <stdio.h>
#include <stdlib.h>
int dynamic_arry_append(int* arry, int* number, int* size);
int main() {
int i, n, size = 3, *arry = NULL, fibarr[size];
printf("Dynamic array, Fibonacci series. \n");
printf("Capture upto element: ");
scanf("%d", &n);
i = 0;
// passing the first elements
fibarr[0] = 0;
fibarr[1] = 1;
fibarr[2] = 1;
while ( i < n ) {
printf("**%d\n",fibarr[0]);
dynamic_arry_append( arry, &fibarr[0], &size );
fibarr[0] = fibarr[1];
fibarr[1] = fibarr[2];
fibarr[2] = fibarr[1] + fibarr[0];
i++;
}
for ( i = 0 ; i < size ; i++)
printf("Element %d of the array: %d.\n", i, arry[i]);
return 0;
}
int dynamic_arry_append(int* arry, int* number, int* size) {
int i;
int bacon = *size; // first name i thought of
bacon++;
int *new_addr = realloc(arry, bacon * sizeof(int));
if( new_addr != NULL ) {
arry = new_addr;
arry[bacon-1] = *number;
// printf for easier debugging, or so i thought
for ( i = 0 ; i < bacon ; i++ )
printf("%d\t%d\n", i+1, arry[i]);
printf("\n");
*size = bacon;
} else {
printf("Error (re)allocating memory.");
exit (1);
}
return 0;
}
At least in my mind this works. However, in practice I get funny results:
Dynamic array, Fibonacci series.
Capture upto element: 5
**0 // next fibonacci number
1 5256368
2 5246872
3 1176530273
4 0
**1
1 5256368
2 5246872
3 1768053847
4 977484654
5 1
**1
1 5256368
2 5246872
3 1551066476
4 1919117645
5 1718580079
6 1
**2
1 5256368
2 5246872
3 977484645
4 1852397404
5 1937207140
6 1937339228
7 2
**3
1 5256368
2 5246872
3 1551071087
4 1953724755
5 842231141
6 1700943708
7 977484653
8 3
/* Code::Blocks output */
Process returned -1073741819 (0xC0000005) execution time : 17.886 s
Press any key to continue.
I am really baffled by this error, and after searching around I found no solution...Can anyone help? Thank you very much.
#include <stdio.h>
#include <stdlib.h>
int * dynamic_array_append(int * array, int size);
int main() {
int i, n, size=0, *array = NULL;
printf("Dynamic array, Fibonacci series. \n");
printf("Capture upto element: ");
scanf("%d", &n);
for (i=0 ; i<n ; i++)
array = dynamic_array_append(array, i);
for (i=0 ; i<n ; i++)
printf("array[%d] = %d\n", i, array[i]);
return 0;
}
int * dynamic_array_append(int * array, int size)
{
int i;
int n1, n2;
int new_size = size + 1;
int * new_addr = (int *) realloc(array, new_size * (int)sizeof(int));
if (new_addr == NULL) {
printf("ERROR: unable to realloc memory \n");
return NULL;
}
if (size == 0 || size == 1) {
new_addr[size] = size;
return new_addr;
}
n1 = new_addr[size-1];
n2 = new_addr[size];
new_addr[new_size-1] = new_addr[new_size-2] + new_addr[new_size-3];
return new_addr;
}
/*
Output:
Dynamic array, Fibonacci series.
Capture upto element: 10
array[0] = 0
array[1] = 1
array[2] = 1
array[3] = 2
array[4] = 3
array[5] = 5
array[6] = 8
array[7] = 13
array[8] = 21
array[9] = 34
*/
Points to note:
The newly (re)allocated array should be returned back to main and stored in a pointer-to-int (or) pass pointer-to-pointer-to-int and update it accordingly once after reallocing
The fibarr is not needed. It doesn't solve any problem.
You don't have to pass the size and the number. Just send the size and it will pick the n-1 and n-2 to calculate n.
This is considered to be highly inefficient. Because if you know the n then you can allocate memory for n integers in one shot and calculate the fib series.
The problem may be that the arry pointer variable is passed by value to the function dynamic_arry_append. That means, that changes that you make to the arry variable within that function will not be reflected by any variables outside of that function. For example:
int *a = NULL;
someFunc(a);
// a will still be NULL here no matter what someFunc does to it.
You should declare your fibarr as a pointer (so name it differently) not an array. And you should pass to your dynamic_arry_append the address of that pointer, like &fibarr. And you should initialize fibarr in your main with calloc. At last you should dynamically update (and keep, and pass) the size of the allocated array.
You are not returning the new address of the array... and you are reading/writing not your memory. Run the program with all error messages under debugger and you'll see the problem is in this line:
dynamic_arry_append( arry, &fibarr[0], &size );
I want to declare a three dimensional array of strings
where each row has two strings.
Here I am able to declare it:
char *szArray[][2] = {
{"string1", "string2"},
{"string3", "string4"},
{"string5", "string6"},
{0, 0}
};
I want to do the same thing but number of rows are dynamically allocated.
Here you can assume 1st dimension is dynamically decided.
2nd dimension is 2,
3rd dimension is 25.
Tried in many ways no luck :(.
Thanks in advance !
First what you have in your question is not a 3-dimensional array of char but a 2-dimensional array of pointers to char. In C, pointers and arrays are not the same thing.
To answer your question, the simplest way in modern C, C99, is to use variable length arrays, VLA, for your purpose. For a 3-dimensional array you'd do
char szData[x][y][z];
where x, y and z are variables or expressions that are only determined at run time when you hit that declaration.
The inconvenience of VLA are that you have to be careful that they don't become too large for your stack, and that you have to initialize them by assignment to the individual entries (here by a nested for-loop).
To stay with your example of 2-d array of strings
char* szData[x][y];
and then you'd have to assign either individual strings to each of the pointers or to set them to 0.
for (size_t i = 0; i < x; ++i)
for (size_t j = 0; j < y; ++j)
szData[i][j] = 0;
int n = 0 ;
printf("Enter the number of rows\n");
scanf("%d",&n);
char *** szData = (char *** )malloc(n * sizeof(char ** )) ;
//Allocate memroy for each row
for(int i = 0 ; i < n ; i++ )
{
szData[i] = (char ** ) malloc(2 * sizeof(char * )) ;
for ( int j = 0 ; j < 2 ; j++ )
{
szData[i][j] = (char *) malloc (25 * sizeof(char));
}
}
//Assign some data
for( int i = 0 ; i < n ; i++ )
{
sprintf(szData[i][0],"string%d", i);
sprintf(szData[i][1],"string1%d", i);
}
//print all the elements
for( int i = 0 ; i < n ; i++ )
{
printf("%s\n",szData[i][0]);
printf("%s\n",szData[i][1]);
}
//free memory here
for(int i = 0 ; i < n ; i++ )
{
for ( int j = 0 ; j < 2 ; j++ )
{
delete szData[i][j];
}
}
for(int i = 0 ; i < n ; i++ )
{
delete szData[i];
}
delete szData;
I didnt get any error:
#include <stdio.h>
int main(){
char *szArray[][2] = { {"string1", "string2"}, {"string3", "string4"}, {"string5", "string6"}, {0, 0} };
printf("%s\n", szArray[0][0]);
printf("%s\n", szArray[2][0]);
}
Here is the output:
$ gcc test.c
$ ./a.exe
string1
string5
But you cannot print szArray[3][0][0] because it is 0, if you want to initialize the value to whatever, you can set to "\0" instead of just 0
Ok, no compiler here to double check, but you can do this a number of ways. The most straight forward would be to declare a Vector of char[25][3] and let c++ do it for you. Something like Vector foo and then just push_back your dynamic elements. Without a compiler though, I'm not 100% certain that this would work as there are funky rules when it comes to multi-dimensional arrays. You can always undimensionalize your array as a first pass just to get it working as well - something like a Vector and start adding how ever you want. You can also do the vector of vector of char approach and have jagged arrays of chars where and of the 3 dimensions can be dynamic, and this would probably even be more memory efficent. Lots of choices here.