Why am I getting a segmentation fault with this code?
/* solver.h header file */
10 struct options{
11 unsigned int one:1, two:1, three:1, four:1, five:1, six:1, seven:1, eight:1, nine:1;
12 };
13
14
15 /* structure to describe a cell */
16 struct cell{
17 short value;
18 struct options open_options;
19 };
solver.c:
5 #include <stdio.h>
6 #include "solver.h"
7
8
9
10
11
12 int main(){
13 struct cell board [9][9];
14 int i=0,j=0;
15
16
17 for(i = 1; i<10; i++)
18 for(j = 1; j<10; j++)
19 (board[i][j]).value = j;
20
21 for(i = 1; i<10; i++){
22 for(j = 1; j<10; j++)
23 printf(" %d",(board[i][j]).value);
24 printf("\n");
25 }
26 return 0;
27 }
output:
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Segmentation fault: 11
Arrays are indexed from 0, so the loops should be for(i = 0; i<9; i++) and not for(i = 1; i<10; i++)
In your case, you probably override part of the stack, but in general, going out of boundaries results in undefined behavior.
some_type array[9]; defines array to be an array of 9 elements, with subscripts going from 0 to 8 inclusive. You can't use array[9].
board[9][9] will contain elements with indices in the range 0...8, not 1...9. When you assigned to board[9][whatever], you actually overwrote memory that didn't belong to you, and this happened to cause the program to explode when return 0 handed control back to the C runtime and it started walking its structures to perform shut down.
Related
The question is:
Given a 2-D array A of size M x N . Write a program in C to print A in spiral form starting from bottom right corner in anti-clockwise direction. There must be space after each element. Note the following
1<=M
N<=20
0<=Aij<=1000
Example:
Input
1 8
1 2 3 4 5 6 7 8
Output
8 7 6 5 4 3 2 1
Input
3 3
1 2 3
4 5 6
7 8 9
Output
9 6 3 2 1 4 7 8 5
I have tried it....can anyone help me with this.
#include<stdio.h>
int main()
{
int m,n;
scanf("%d%d",&m,&n) ;
int matrix[m][n] ;
int last_row = (m-1) ;
int last_column = (n-1) ;
int first_row = 0;
int first_column = 0;
int i=0,j=0,r=0,c=0;
while(i<m)
{
while(j<n)
{
scanf("%d",&matrix[i][j]) ;
++j;
} j=0;
++i;
}
while(first_column <=last_column && first_row <= last_row)
{ for( r=last_row ; r>=first_row ; --r)
{ c=last_column;
printf("%d ",matrix[r][c]) ;
} --last_column;
for (c=last_column; c>=first_column ; --c)
{
r=first_row;
printf("%d ",matrix[r][c]) ;
} ++first_row ;
for(r=first_row; r<=last_row; r++)
{
c=first_column;
printf("%d ",matrix[r][c]) ;
} ++first_column;
for(c=first_column ; c<=last_column ; c++)
{
r=last_row;
printf("%d ",matrix[r][c]) ;
} --last_row;
}
return 0;
}
The current output which I am getting is :
Input
8 1
1
2
3
4
5
6
7
8
Output
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
Input
1 8
1 2 3 4 5 6 7 8
Output
8 7 6 5 4 3 2 1 2 3 4 5 6 7
I am trying to 4x4 input in an 5x5 array and get the sum of each lines on the fifth lines.
I'm sure if you read my code below, you'll know what I am trying to talk about.
For example if I type in:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The expected Result should be:
1 2 3 4 10
5 6 7 8 26
9 10 11 12 42
13 14 15 16 58
28 32 36 40 136
Instead, I am getting a result like:
1 2 3 4 10
5 6 7 8 32792
9 10 11 12 42
13 14 15 16 58
28 32 36 40 -501277720
I thought about why I get these random values, but couldn't find a solution. Why am I getting these values and what can I do to solve it?
#include <stdio.h>
int main ()
{
int gradeArr[5][5];
int i,j;
printf("Input grades:\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
scanf("%d",&gradeArr[i][j]);
gradeArr[i][4] += gradeArr[i][j];
}
}
printf("%d\n", gradeArr[1][4]);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
gradeArr[4][i] += gradeArr[j][i];
}
gradeArr[4][4] += gradeArr[4][i];
}
printf("Result: \n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%d ",gradeArr[i][j]);
}
printf("\n");
}
return 0;
}
You need to initialize the array.
Try
int gradeArr[5][5] = {0};
I am trying to make a program that takes 3 inputs from the user: row/columns count and number range start and number range end.
In my case and for example lets say 4 1 16 so it means 4 rows and columns print numbers from 1-16.
I have a problem accomplishing this.
#include <stdio.h>
#include <stdlib.h>
int num1,num2,count,i,y;
int main()
{
count = 4;
num1 = 1;
num2 = 16;
for(i=1; i<=count; i++){
for(y=num1; y<=num2; y++){
printf("%d ",y);
}
printf("\n");
}
return 0;
}
The output is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Whereas I want my output to be:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
for(y=1; y<=count; y++){
printf("%d ",num1++);
}
That way you will ensure that all the numbers will be printed. you can use num1 variable that you have declared and initialized with 1.
Don't use unnecessary global variables. And keep the declarations close to where you use them. That way you won't have to look in the top of the page to check the type or if you initialized or not.
for(i=1; i<=count; i++){
printf("%d ",i);
if( i % 4 == 0)
printf("\n");
}
Print 4*(i-1)+y instead of y:
printf("%d ",4*(i-1)+y);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This code is designed by someone to change array [a1 a2...am b1 b2..bn ] to the array [b1 b2 ..bn a1 a2..am], but it involves the greatest common divisor which I can't get the point.
void Exchange(int a[],int m,int n,int s){
int p=m,temp=m+n;int k=s%p;
while(k!=0){temp=p;p=k;k=temp%p;}
for(k=0 ; k<p ;k++){ //below is where i cant't understand
temp=a[k];i=k;j=(i+m)%(m+n);
while(j!=k)
{a[i]=a[j];i=j;j=(j+m)%(m+n);}
a[i]=temp;
}
};
EDIT: "Properly" indented:
void Exchange(int a[], int m, int n, int s) {
int p = m, temp = m + n, k = s % p;
while (k != 0) {
temp = p;
p = k;
k = temp % p;
}
for (k = 0 ; k < p; k ++) { // below is where i cant't understand
temp = a[k];
i = k;
j = (i + m) % (m + n);
while (j != k) {
a[i] = a[j];
i = j;
j = (j + m) % (m + n);
}
a[i] = temp;
}
};
The code is using a single value of overhead to implement array rotation. If the lengths are mutually prime, a single pass suffices. If not, you have to repeat the shift cycle by the GCD of the lengths
I said earlier that there are other questions on SO that cover this. A look found SO 3333-3814 which deals with a single rotation. I did some messing with code to support that a while ago, demonstrating the need for GCD, but I didn't previously post it.
Here's the code — it uses C99 VLAs — variable length arrays.
#include <stdio.h>
static int gcd(int x, int y)
{
int r;
if (x <= 0 || y <= 0)
return(0);
while ((r = x % y) != 0)
{
x = y;
y = r;
}
return(y);
}
static void dump_matrix(int m, int n, int source[m][n])
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
printf("%4d", source[i][j]);
putchar('\n');
}
}
static void init_matrix(int m, int n, int source[m][n])
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
source[i][j] = (i + 1) * (j + 2);
}
}
static void rotate_1col(int n, int vector[n], int z)
{
z %= n;
if (z != 0)
{
int c = gcd(n, z);
int s = n / c;
for (int r = 0; r < c; r++)
{
int x = r;
int t = vector[x];
for (int i = 0; i < s; i++)
{
int j = (x + z) % n;
int v = vector[j];
vector[j] = t;
x = j;
t = v;
}
}
}
}
static void rotate_cols(int m, int n, int source[m][n], int z)
{
for (int i = 0; i < m; i++)
rotate_1col(n, source[i], z);
}
int main(void)
{
int m = 3;
for (int n = 2; n < 9; n++)
{
int source[m][n];
for (int z = 0; z <= n; z++)
{
init_matrix(m, n, source);
printf("Initial:\n");
dump_matrix(m, n, source);
rotate_cols(m, n, source, z);
printf("Post-rotate %d:\n", z);
dump_matrix(m, n, source);
putchar('\n');
}
}
return 0;
}
The code demonstrates different sizes of rotation on different sizes of array. Example sections of the output:
…
Initial:
2 3 4
4 6 8
6 9 12
Post-rotate 1:
4 2 3
8 4 6
12 6 9
…
Initial:
2 3 4 5
4 6 8 10
6 9 12 15
Post-rotate 3:
3 4 5 2
6 8 10 4
9 12 15 6
…
Initial:
2 3 4 5 6 7
4 6 8 10 12 14
6 9 12 15 18 21
Post-rotate 1:
7 2 3 4 5 6
14 4 6 8 10 12
21 6 9 12 15 18
Initial:
2 3 4 5 6 7
4 6 8 10 12 14
6 9 12 15 18 21
Post-rotate 2:
6 7 2 3 4 5
12 14 4 6 8 10
18 21 6 9 12 15
Initial:
2 3 4 5 6 7
4 6 8 10 12 14
6 9 12 15 18 21
Post-rotate 3:
5 6 7 2 3 4
10 12 14 4 6 8
15 18 21 6 9 12
…
Initial:
2 3 4 5 6 7 8 9
4 6 8 10 12 14 16 18
6 9 12 15 18 21 24 27
Post-rotate 4:
6 7 8 9 2 3 4 5
12 14 16 18 4 6 8 10
18 21 24 27 6 9 12 15
Initial:
2 3 4 5 6 7 8 9
4 6 8 10 12 14 16 18
6 9 12 15 18 21 24 27
Post-rotate 5:
5 6 7 8 9 2 3 4
10 12 14 16 18 4 6 8
15 18 21 24 27 6 9 12
Initial:
2 3 4 5 6 7 8 9
4 6 8 10 12 14 16 18
6 9 12 15 18 21 24 27
Post-rotate 6:
4 5 6 7 8 9 2 3
8 10 12 14 16 18 4 6
12 15 18 21 24 27 6 9
…
First of all, to get the result you said you expected, I have set m and n to be half the array size. I also assumed that s would be initialised to zero, in which case, the first while loop does not iterate. Also, there are several declarations missing in your code so my explanation makes some assumptions.
The variable p holds the number of array elements to swap;
// This is to keep the value to be overwritten by the swap
temp=a[k];
// This is the array index of the bottom half element to write the top half element to
i=k;
// this is to get the current index of the top half;
j=(i+m)%(m+n);
// This assignes the bottom index value with the top half value
while(j!=k)
{
// Write top half element to corresponding bottom half element
a[i]=a[j];
// We can now overwrite top half element; this assignes the index at wich to copy the bottom half element
i=j;
// This is to get out of the loop
j=(j+m)%(m+n);
}
// The bottom half element held at the beginning is now written to the top half at the corresponding index
a[i]=temp;
Hope this is the answer you were looking for. I arrived at this result by using a debugger and by stepping in the code line by line. I don't know if you know how to use a debugger but if not, then I highly recommend your lean how to use one; it it time well spent and it returns an awesome dividend :-)
Hello everyone i am having a bit of trouble with a project for my class
We just started learning C and I can't get this assignment working, Its a bubble sorter.
I'm unfamiliar with the syntax as we just learned it, but i think the code itself, except the printValues, is working... not sure yet how to do that one
It has several errors come up but the first is
error: expected expression before 'if'
I am also not sure about the second for statement in `sort()
Intended Output:
Before:
[7 3 9 4 6 1 2 8 5 ]
[3 7 9 4 6 1 2 8 5 ]
[3 7 4 9 6 1 2 8 5 ]
[3 7 4 6 9 1 2 8 5 ]
[3 7 4 6 1 9 2 8 5 ]
[3 7 4 6 1 2 9 8 5 ]
[3 7 4 6 1 2 8 9 5 ]
[3 7 4 6 1 2 8 5 9 ]
[3 4 7 6 1 2 8 5 9 ]
[3 4 6 7 1 2 8 5 9 ]
[3 4 6 1 7 2 8 5 9 ]
[3 4 6 1 2 7 8 5 9 ]
[3 4 6 1 2 7 5 8 9 ]
[3 4 1 6 2 7 5 8 9 ]
[3 4 1 2 6 7 5 8 9 ]
[3 4 1 2 6 5 7 8 9 ]
[3 1 4 2 6 5 7 8 9 ]
[3 1 2 4 6 5 7 8 9 ]
[3 1 2 4 5 6 7 8 9 ]
[1 3 2 4 5 6 7 8 9 ]
[1 2 3 4 5 6 7 8 9 ]
After:
[1 2 3 4 5 6 7 8 9 ]
Here is my code so far:
#include <stdio.h>
#define MAX 9
//function prototypes
void printValues(){
printf("hi");
}//end printValues
void sort(){
int i;
int j;
for (i = 0; i < MAX-1; i++)
for (j = 0; j < MAX -1 - i)
if (*[i] > *[j + 1])
swap(*[j] *[j+1])
}//end sort
void swap(int*h, int*l){
int temp;
temp = *l
*l = *h
*h = temp
}//end swap
int values[] = {7, 3, 9, 4, 6, 1, 2, 8, 5};
int main(){
printf("Before: \n");
printValues();
sort();
printf("After: \n");
printValues();
return(0);
} // end main
I got it working.
Some errors in your code:
using function before declaring it
mixing & and *
missing semicolons
broken loop syntax (why did you put ; after it? That would be a do-nothing loop!)
I also added implementation of the printValues() function
Also, *[i] doesn't really mean anything. You already have array, so just values[i] will do it. You can use ampersand if you want to get it's address (make a pointer).
Also... using a global array for this is not a very good idea. It'd be much better if you passed the array to your sort function as an argument (pointer to it).
#include <stdio.h>
#define MAX 9
int values[] = {7, 3, 9, 4, 6, 1, 2, 8, 5};
//function prototypes
void printValues(){
int i;
printf("[");
for(i = 0; i < MAX; i++) {
printf(" %d ", values[i]);
}
printf("]\n");
}//end printValues
void swap(int* h, int* l){
int temp;
temp = *l;
*l = *h;
*h = temp;
}//end swap
void sort() {
int i;
int j;
for (i = 0; i < MAX-1; i++) {
for (j = 0; j < MAX - 1 - i; j++) {
if (values[j] > values[j + 1])
swap(&values[j], &values[j+1]);
}
}
}//end sort
int main(){
printf("Before: \n");
printValues();
sort();
printf("After: \n");
printValues();
return(0);
} // end main