Error: 'for' loop initial declarations are only allowed in c99 mode - c

I've got this problem where I can only compile using the gcc -std=c99 but however, i need it to compile using c89 aka gcc -Wall. This is part of my code where i use the 'for' loop. Please see if you can help me out thank you in advance.
#include<stdio.h>
int main()
{
int arr[100],i=0,ch;
int n = 1, sum = 0;
printf("Check out our selection! \n");
printf("Airhead - 25 cents\n");
printf("Fun Dip - 40 cents\n");
printf("Gummi Bears - 20 cents\n");
while (n != 0)
{
printf("Insert Coins: ");
scanf("%d",&n);
arr[i++] = n;
}
for(int j=0;j<i;j++)
{ sum = sum + arr[j];
}
......

This is wrong:
for (int j = 0; j < i; j++) {
sum = sum + arr[j];
}
You have to initialize j in beginning of function.
int main() {
int j;
...
for (j = 0; j < i; j++) {
sum = sum + arr[j];
}
}

Related

Why my code is giving error after i submit code

In CodeChef website problem SQUIDRULE it is giving me a runtime error after submitting but when I run the program it is giving me right answer and also run sucessfully.
#include <stdio.h>
int main(void) {
int T, s, a[1000], b[100];
scanf("%d", &T);
while (T--) {
scanf("%d", &s);
for (int i = 1; i <= s; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= s; i++) {
int sum = 0, c;
for (int j = 1; j <= s; j++) {
sum = sum + a[j];
}
c = sum - a[i];
b[i] = c;
}
for (int i = 1; i <=s; i++) {
for (int j = i + 1; j <=s; j++) {
if (b[i] > b[j]) {
int c;
c = b[i];
b[i] = a[j];
b[j] = c;
}
}
}
printf("%d\n",b[s]);
}
return 0;
}
From my top comments ...
In your second set of nested for loops, which take O(n^2) time, you are sorting [using slow bubble sort instead of qsort?]. At the end, you only want to print b[s].
AFAICT, you do not need to sort at all. You're just looking for the max value of the array, so you can calculate that in O(n) time.
Further, sum is invariant across all iterations of i. So, you can move the j loop above the second i and only calculate it once. Once again, you can reduce running time from O(n^2) to O(n)
As WeatherVane pointed out, you need [at least] 10,000 entries in a.
Additionally, you can eliminate the separate for loop for calculating the sum by doing this in the scanf loop.
Here is a refactored version:
#include <stdio.h>
#define AMAX (100000 + 10)
int s, a[AMAX];
int sum;
int
fast(void)
{
int mx = sum - a[0];
for (int i = 1; i < s; i++) {
int c = sum - a[i];
if (c > mx)
mx = c;
}
return mx;
}
int
main(void)
{
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &s);
sum = 0;
for (int i = 0; i < s; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
int v2 = fast();
printf("%d\n",v2);
}
return 0;
}

Why is my code producing a segmentation fault?

So im working on this program that is supposed to take the pointer to an array and the array’s size (number of elements in the array)
as arguments, finds the place the index of the outlier, fixes the array in place (that is puts the outlier to a
place it is supposed to be), and returns the old index where the outlier was found. i finished my code but for some reason, somewhere in my main function its telling me there is a segmentation fault, i know its in my main function because it compiled and ran fine when it was just the original code. heres the code;
#include <stdio.h>
long long int fix_sorted_array(double* arr, unsigned long n)
{
double temp;
int i, j;
for ( i = 0; i < n - 1; i ++)
{
if (arr[i] > arr[i + 1])
{
for ( j = i + 1; j > 0; j --)
{
if (arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
return i + 1;
}
}
return -1;
}
int main()
{
int n;
int j;//declared variables
double arr[n];
printf("Enter elements of array : \n");
for ( int i = 0; i < n; i ++)
{
scanf("%lf", &arr[i]);
}
printf("Return index : %lld\n",fix_sorted_array (&arr[n], n));
printf("Array after : \n");
for ( j = 0; j < n; j ++)
{
printf("%.2lf", arr[j]);
}
}
You're passing an address outside the array to the function in this line:
printf("Return index : %lld\n",fix_sorted_array (&arr[n], n));
You want to pass the address of the start of the array, not the end, so it should be:
printf("Return index : %lld\n",fix_sorted_array (arr, n));
You also need to initialize n before you declare the array.
printf("How many numbers? ");
scanf("%d", &n);
double arr[n];
You have never accepted the value of n. Below code might help.
#include <stdio.h>
long long int fix_sorted_array(double* arr, unsigned long n)
{
double temp;
int i, j;
for ( i = 0; i < n - 1; i ++)
{
if (arr[i] > arr[i + 1])
{
for ( j = i + 1; j > 0; j --)
{
if (arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
return i + 1;
}
}
return -1;
}
int main()
{
int n;
int j;//declared variables
printf("Enter the number of elements in arrays");
scanf("%d",&n); // initialize the values of n
double arr[n];
printf("Enter elements of array : \n");
for ( int i = 0; i < n; i ++)
{
scanf("%lf", &arr[i]);
}
printf("Return index : %lld\n",fix_sorted_array (&arr[0], n)); // Also pass the value of starting index in array i.e. `arr[0]`
printf("Array after : \n");
for ( j = 0; j < n; j ++)
{
printf("%.2lf", arr[j]);
}
}

array manipulation question on hackerrank

My code shows segmentation fault on hackerrank.What will happen if I use long long int?
Here is the link https://www.hackerrank.com/challenges/crush/problem?isFullScreen=true
My code is:
#include <stdio.h>
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int queries[m][3];
for (int i = 1; i <= m; i++) {
scanf("%d", &queries[i][1]);
scanf("%d", &queries[i][2]);
scanf("%d", &queries[i][3]);
}
int a[n];
for (int i = 1; i <= n; i++)
a[i] = 0;
for (int i = 1; i <= m; i++) {
for (int j = queries[i][1]; j <= queries[i][2]; j++)
a[j] = a[j] + queries[i][3];
}
int max;
max = 0;
for (int i = 1; i <= n; i++) {
if (max < a[i])
max = a[i];
}
printf("%d", max);
return 0;
}
Array index out of bounds at .
a[j]=a[j]+queries[i][3];
because Array index 3 is past the end of the array (which contains 3 elements) thus 2 is the last index.
and when using the for loops to access the array you might need to start from 0
Code
#include<stdio.h>
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int queries[m][3];
for( int i=0;i<m;i++)
{
scanf("%d",&queries[i][0]);
scanf("%d",&queries[i][1]);
scanf("%d",&queries[i][2]);
}
int a[n];
for(int i=0;i<n;i++)
a[i]=0;
for(int i=0;i<m;i++)
{
for(int j=queries[i][0];j<=queries[i][1];j++)
a[j]=a[j]+queries[i][2];
}
int max;
max=0;
for(int i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("%d",max);
return 0;
}
You have array index out of bounds error at this line:
a[j] = a[j] + queries[i][3];
What value can i take?
To avoid similar errors in the future you may want to consider usage of tools for static code analysis. For example, cppcheck is a free and open source tool that can detect this error:
stackoverflow/c-issue.c:23:34: error: Array 'queries[2147483648][3]' accessed
at index queries[*][3], which is out of bounds.
[arrayIndexOutOfBounds]
a[j] = a[j] + queries[i][3];
In every loop you are passing boundaries of your array which will cause undefined behavior.
when you declare int arr [num] , if you access arr[x] while x=>num , you are passing boundaries of your array and that will cause undefined behavior , which means you can't predict what will happen.
look
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int queries[m][3];
for (int i = 0; i < m; i++) {
scanf("%d", &queries[i][0]);
scanf("%d", &queries[i][1]);
scanf("%d", &queries[i][2]);
}
int a[n];
for (int i = 0; i < n; i++)
a[i] = 0;
for (int i = 0; i < m; i++) {
for (int j = queries[i][0]; j <= queries[i][1]; j++)
a[j] = a[j] + queries[i][2];
}
int max;
max = 0;
for (int i = 0; i < n; i++) {
if (max < a[i])
max = a[i];
}
printf("%d", max);
return 0;
}

The reverse of all numbers on the 5th column on matrix [closed]

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
I have a C problem where I need to reverse all the numbers on the 5th column of a 2x5 matrix.
So if I have
1 2 3 4 89
3 8 6 8 91
This will become
1 2 3 4 98
3 8 6 8 19
The code I've written so far is:
#include <stdio.h>
void inverse() {
int reversedNumber = 0, remainder, mat[10][10], i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
while (mat[i][j] != 0) {
remainder = mat[i][j] % 10;
reversedNumber = reversedNumber * 10 + remainder;
mat[i][j] /= 10;
}
}
printf("Reversed Number = %d", reversedNumber);
}
void main()
{
int mat[10][10], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
inverse(mat[1][5]);
}
After running this I get a ridiculously large number! What should I modify?
There are number of things that can be improved in the code.
First of all you have to set reversedNumber zero inside the innermost loop, this is the reason you get large numbers.
You pass an argument to the function, but the definition is incorrect for the same.
Also, you have stated that you only need to reverse the 5th column, better make call to a function that reverses a single number.
#include<stdio.h>
int inverse(int num) {
int reversednum = 0;
while(num){
reversednum = reversednum*10 + num%10;
num /= 10;
}
return reversednum;
}
void main(){
int mat[10][10],i,j;
printf("Enter your matrix\n");
for(i=0;i<2;i++)
for(j=0;j<5;j++){
scanf("%d",&mat[i][j]);
}
printf("\nHere is your matrix:\n");
for(i=0;i<2;i++){
for(j=0;j<5;j++){
printf("%d ",mat[i][j]);
if(j == 4) mat[i][j] = inverse(mat[i][j]);
}
printf("\n");
}
}
mistakes in your program:
-your function doesnt expect anything i.e empty parameter but you are sending matrix as parameter.
do not unnecessarily use matrix of size [10][10] when your matrix of 2*5
send 'mat' as parameter(i.e address of your matrix) to function inverse
#include <stdio.h>
int main() {
//code
int mat[10][10], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
if(j==4) //only for 5th column
{
// int temp=mat[i][j]; // can use temporary variable instead of changing actual value matrix (better option)
int remainder, reverse =0;
while(mat[i][j]>0)
{
remainder=mat[i][j]%10;
reverse=reverse*10 + remainder;
mat[i][j]=mat[i][j]/10;
}
mat[i][j]=reverse;
}
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}
Edited: modified code from the question
#include <stdio.h>
void inverse(int mat1[2][5]) {
int i, j;
for (i = 0; i < 2; i++){
int j=4;
int reversedNumber = 0, remainder=0;
while (mat1[i][j] > 0) {
remainder = mat1[i][j] % 10;
reversedNumber = reversedNumber * 10 + remainder;
mat1[i][j] /= 10;
}
printf("Reversed Number = %d\n",reversedNumber);
}
}
void main()
{
int mat[2][5], i, j;
printf("Enter your matrix\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 5; j++) {
scanf("%d", &mat[i][j]);
}
printf("\nHere is your matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 5; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
inverse(mat);
}
hope this helps.
You have to pass the matrix to the inverse function so that the matrix (mat) can be modified. If you declare a separate mat array inside inverse then that's a different scope. You also have to figure out how many digits there are in the number. You can use <math.h> functions, or the example below uses basic calculations.
void inverse(int mat[2][5])
{
for(int i = 0; i < 2; i++)
for(int j = 0; j < 5; j++)
{
int n = mat[i][j];
int digits = 0;
while(n > 0)
{
digits++;
n /= 10;
}
n = mat[i][j];
int rev = 0;
while(digits > 0)
{
int x = n % 10;
for(int c = 0; c < digits - 1; c++)
x *= 10;
rev += x;
n /= 10;
digits--;
}
mat[i][j] = rev;
}
}
int main(void)
{
int mat[2][5] = {
1, 2, 3, 4, 89,
3, 8, 6, 8, 91 };
inverse(mat);
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 5; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
return 0;
}

Shell sort of integer array in C

I am new to C and trying to learn shell sorting.I am trying to sort an integer array in ascending order.Here is my code-
#include <stdio.h>
main()
{
int a[] = {1, 9, 7, 4, 8, 6, 7,2,1,6 };
int n =10; //array length
for (int c = (n / 2); c > 0; c = c / 2)
{
for (int i = c; c < n; i++)
{
int t = a[i];
int j;
for (j = i;( j >= c) && (t < a[j - c]); j = j - c)
{
a[j] = a[j - 1];
}
a[j] = t;
}
}
for (int i = 0; i <= 9; i++)
{
printf("%d ", a[i]);
}
}
On compiling this code in Visual Studio Express an error message comes asking either to close or debug and the output terminal not shows any output.I can't figure out what is wrong in this code.I would appreciate if someone could explain, and perhaps point me to a solution that would do what I want.
Your code is almost right, just replace for (int i = c; c < n; i++) with for (int i = c; i < n; i++)
'c' will always be lesser than 'n' that's why your code is falling into infinite loop.
try to add this code....
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30];
int i,j,k,tmp,num;
printf("Enter total no. of elements : ");
scanf("%d", &num);
for(k=0; k<num; k++)
{
printf("\nEnter %d number : ",k+1);
scanf("%d",&arr[k]);
}
for(i=num/2; i>0; i=i/2)
{
for(j=i; j<num; j++)
{
for(k=j-i; k>=0; k=k-i)
{
if(arr[k+i]>=arr[k])
break;
else
{
tmp=arr[k];
arr[k]=arr[k+i];
arr[k+i]=tmp;
}
}
}
}
printf("\t**** Shell Sorting ****\n");
for(k=0; k<num; k++)
printf("%d\t",arr[k]);
getch();
return 0;
}

Resources