What is the difference between Array[n] ; and Array[ ]={ }; - arrays

#include <stdio.h>
int main() {
int n, i, j, k, l;
int temp;
printf("Enter how many element on the array : ");
scanf("%d", &n);
int arr1[100] = {};
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d \t", arr1[i]);
}
}
My code for sorting an array in ascending order works properly. And it doesn't have any error but when I am changed the array size then the code doesn't work properly and has an error called stack smashing detected. What causes this problem?
#include <stdio.h>
int main() {
int n, i, j, k, l;
int temp;
printf("Enter how many element on the array : ");
scanf("%d", &n);
int arr1[] = {};
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d \t", arr1[i]);
}
}

Neither int arr1[100] = {}; nor int arr1[] = {}; is valid C code.
The program compiles because your compiler implements GNU extensions that allow empty initializers and zero length arrays.
The reason your program no longer works when you remove the length 100 is the array becomes too short for the elements you try and store into it.
You probably meant to write int arr1[n] = {}; which does not compile because VLAs (variable sized arrays) cannot have an initializer.
Here is a modified version:
#include <stdio.h>
int main() {
int n, i, j, k, l;
printf("Enter how many element on the array : ");
if (scanf("%d", &n) != 1 || n <= 0) {
fprintf(stderr, "invalid size\n");
return 1;
}
int arr1[n];
for (i = 0; i < n; i++) {
if (scanf("%d", &arr1[i]) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
}
for (j = 0; j < n; j++) {
for (k = j + 1; k < n; k++) {
if (arr1[j] > arr1[k]) {
int temp = arr1[j];
arr1[j] = arr1[k];
arr1[k] = temp;
}
}
}
for (i = 0; i < n; i++) {
printf("%d%c", arr1[i], "\t\n"[i == n - 1]);
}
return 0;
}

Related

Code not running fully in VsCode but working in online compilers

This is the code i am talking about. It checks if a matrix is sparse or not . If it is then it changes it to sparse form and transposes it. When I run it with Vscode it runs and displays till it is sparse matrix but doesn't show its sparse form and transpose form but on online compiler it runs successfully till the end.
#include <stdio.h>
int checksparse();
int changematrix();
int main()
{
int a[100][100];
int i, j, r, c;
printf("Enter number of rows and columns in the matrix\n");
scanf("%d%d", &r, &c);
printf("\nEnter the elements in the matrix");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("\na[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
int check = checksparse(a, r, c);
if (check == 1)
{
printf("\nIts a sparse matrix\n");
changematrix(a, r, c);
}
else if (check == 0)
{
printf("\nIts not a sparse matrix");
}
return 0;
}
int checksparse(int array[100][100], int r, int c)
{
int counter = 0, flag = 1;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (array[i][j] == 0)
counter++;
}
}
if (counter > ((r * c) / 2))
{
return flag;
}
else
{
flag = 0;
return flag;
}
}
int changematrix(int array[100][100], int r, int c)
{
int b[100][100], temp, counter = 0;
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (array[i][j] != 0)
{
b[temp][0] = i;
b[temp][1] = j;
b[temp][2] = array[i][j];
temp++;
counter++;
}
}
}
printf("\nSparse Matrix : \n");
for (int i = 0; i < counter; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", b[i][j]);
printf("\t");
}
printf("\n");
}
printf("\nTranspose of Sparse Matrix : \n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < counter; j++)
{
printf("%d ", b[j][i]);
printf("\t");
}
printf("\n");
}
return 0;
}

Runtime error in a C program to find an index of an element

This program prints the index of element after sorted. Here is the code:
#include<stdio.h>
#include<limits.h>
int n;
int value[10000];
int index[10000];
int enable[10000];
//return the element which is smallest and never selected
int min() {
int i, tmp=INT_MAX;
for (i = 0; i < n; i++) {
if (enable[i] == 0 && tmp > value[i] )
tmp = value[i];
}
return tmp;
}
void solve() {
int i,j;
int tmp;
for (i= 0; i < n; i++) {
tmp = min();
for (j = 0; j < n; j++) {
if (enable[j] == 0 && tmp == value[j] ) {
index[j] = i + 1;
enable[j] = 1;
}
}
}
}
void print() {
int i = 0;
for (; i < n-1; i++)
printf("%d ", index[i]);
printf("%d\n", index[i]);
}
int main() {
int i;
while (scanf("%d", &n) != EOF) {
for (i = 0; i < n; i++) {
scanf("%d", &value[i]);
}
solve();
print();
}
return 0;
}
It worked well on my computer, but the online judge shows runtime error. I could not figure it out where it went wrong, maybe my test is not enough.

Getting a SIGSEGV error in my C program

This is my code for the problem.
I used a dynamic programming approach, and my answer is coming out to be correct. But I am getting a Runtime Error of SIGSEGV, may be because of array index and I am unable to figure out how and where?
If you can figure out what and where is the problem, please let me know.
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int dynp(int i, int j);
int v[] = {-1, 0, 1, 0};
int h[] = {0, 1, 0, -1};
int ROW, COL;
#define INF 1000000
int minOfTwo(int one, int two){
if (one<two) {
return one;
}else{
return two;
}
}
int matrix[190][190];
int dp[190][190];
int main(int argc, const char * argv[]) {
int t;
scanf("%d", &t);
int i,j;
char s[190];
while (t--) {
scanf("%d %d", &ROW, &COL);
for (i = 0; i<ROW; i++) {
scanf("%s", s);
for (j = 0; j<COL; j++) {
matrix[i][j] = s[j] - 48;
dp[i][j] = INF;
}
}
for (i = 0; i<ROW; i++) {
for (j = 0; j<COL; j++) {
if (dp[i][j] == INF) {
if (matrix[i][j] == 1) {
dp[i][j] = 0;
}
else{
dp[i][j] = dynp(i, j);
}
}
}
}
for (i = 0; i<ROW; i++) {
for (j = 0; j<COL; j++) {
printf("%d ", dp[i][j]);
}
printf("\n");
}
}
return 0;
}
int dynp(int i, int j)
{
if (dp[i][j] != INF) {
return dp[i][j];
}
else{
if (matrix[i][j] == 1) {
dp[i][j] = 0;
return dp[i][j];
}
else{
int k;
for (k = 0; k<4; k++) {
int newi = i + v[k], newj = j + h[k];
if (newi < ROW && newj < COL && newi>=0 && newj>=0) {
dp[i][j] = minOfTwo(dp[i][j], 1 + dynp(newi, newj));
}
}
return dp[i][j];
}
}
}
At the first look, in your code,
scanf("%s", s);
for (j = 0; j<COL; j++) {
matrix[i][j] = s[j] - 48;
looks problematic. With the length of s lesser than the value of COL and s being an automatic local variable not initialized explicitly, you'll be accessing allocated but uninitialized memory location.
You should change the looping condition to something like
scanf("%189s", s); //to avoid overflow
int len = strlen(s);
for (j = 0; (j<COL) && (j < len); j++, len--) {
matrix[i][j] = s[j] - 48;

Unhandled exception in c

my code is compiled well, and executed. But I get this error after I enter the sortType value: "Unhandled exception at 0x52a56af2 (msvcr90d.dll) in ALINUR_CAGLAYAN_LAB6.exe: 0xC0000005: Access violation writing location 0x00000000."
Here's my code:
#include <stdio.h>
int sorting(int *liverpool8, int *besiktas0, int *hahaha);
void main()
{
int *numbers;
int length;
int sortType=0;
int i;
printf("Enter the length of array:");
scanf("%d",&length);
numbers = (int*)malloc(length*sizeof(int));
for (i = 0; i < length; ++i)
{
printf("%d. element: ", i+1);
scanf("%d", &numbers[i]);
}
printf("\nPlease select one of the following functions:\n1)Ascending order\n0)Descending order");
scanf("%d", sortType);
sorting(*numbers, &length, &sortType);
printf("The numbers arranged in the order as you entered are given below\n");
for (i = 0; i < length; ++i)
{
printf("%d\n", numbers[i]);
}
system("pause");
}
int sorting(int *numbers, int *length, int *sortType)
{
int j, i, a;
if(sortType == 1)
{
for (i = 0; i < length; ++i)
{
for (j = i + 1; j < length; ++j)
{
if (numbers[i] > numbers[j])
{
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
}
else if(sortType == 0)
{
for (i = 0; i < length; ++i)
{
for (j = i + 1; j < length; ++j)
{
if (numbers[i] < numbers[j])
{
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
return *numbers;
}
}
There are multiple errors in your code.
The header file for using malloc is not defines.
scanf needs to access addresses and not variables.
The array name decays to a pointer so using * for an array is double dereferencing
To access value in pointer you have to use *, the dereferencing pointer.
I have attached the corrected code.
#include <stdio.h>
#include <stdlib.h>
int sorting(int *liverpool8, int *besiktas0, int *hahaha);
void main()
{
int *numbers;
int length;
int sortType=0;
int i;
printf("Enter the length of array:");
scanf("%d",&length);
numbers = (int*)malloc(length*sizeof(int));
for (i = 0; i < length; ++i)
{
printf("%d. element: ", i+1);
scanf("%d", &numbers[i]);
}
printf("\nPlease select one of the following functions:\n1)Ascending order\n0)Descending order");
scanf("%d", &sortType);
sorting(numbers, &length, &sortType);
printf("The numbers arranged in the order as you entered are given below\n");
for (i = 0; i < length; ++i)
{
printf("%d\n", numbers[i]);
}
system("pause");
}
int sorting(int *numbers, int *length, int *sortType)
{
int j, i, a;
if(*sortType == 1)
{
for (i = 0; i < *length; ++i)
{
for (j = i + 1; j < *length; ++j)
{
if (numbers[i] > numbers[j])
{
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
}
else if(sortType == 0)
{
for (i = 0; i < *length; ++i)
{
for (j = i + 1; j < *length; ++j)
{
if (numbers[i] < numbers[j])
{
a = numbers[i];
numbers[i] = numbers[j];
numbers[j] = a;
}
}
}
return *numbers;
}}

C array nondecreasing order insert value

I am writing a program that arranges an array in nondecreasing order; then, it inserts a value into the sequence. I can easily get numbers in the beginning and the middle of the array, but whenever I add a number that should go at the end, I keep getting 0. Where am I going wrong?
#include <stdio.h>
int main()
{
int array[10];
int i, j, n, m, temp, key, pos;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Sorted list is\n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted X:\n");
scanf("%d", &key);
for (i = 0; ; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
array[pos] = key;
printf("Final list is:\n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
}
If the number to be entered is larger than all elements the following loop poses an issue.
...
for (i = 0; ; i++)
{
if (key < array[i])
{
pos = i;
break;
}
}
If the number is largest,then the pos will be junk and not n.The default value of an array is junk.
Replace it with this.
for (i = 0;i<n ; i++)
{
if (key < array[i])
{
break;
}
}
pos = i;
...

Resources