C programming printf not printing when used nested loop - c

I'm learning about multidimensional array in C programming. But the printf function is not working. Here is my code:
#include <stdio.h>
int main (void)
{
int array[2][3][4];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
for (int k = 0; k < 5; k++)
{
array[i][j][k] = k;
printf("array[%d][%d][%d] = %d\n", i, j, k, array[i][j][k]);
};
};
};
printf("Loop is finished!");
return 0;
}

You are going to loop out of bounds.
Take the first dimension, 2, your loop is < 3.... so its going to use indexes 0 1 2. only 0 and 1 are valid. change your loops to i < 2, j < 3 and k < 4 respectively.

This program will not give result since it having lots of Syntax errors. you need not to be give ;- semicolon after for loop
syntax for For Loop is:
FOR( initialization expression;condition expression;update expression)
{
\\Loop content comes here
}
And also C will not permit Instant Declaration, variables should be declare # the declaration section.
Your Program can be improved by applying these changes, then it gives output. the code snippet will be like the following:
#include <stdio.h>
int main ()
{
int array[3][4][5];
int i,j,k;
for ( i = 0; i < 3; i++)
{
for ( j = 0; j < 4; j++)
{
for (k = 0; k < 5; k++)
{
array[i][j][k] = k;
printf("array[%d][%d][%d] = %d\n", i, j, k, array[i][j][k]);
}
}
}
printf("Loop is finished!");
return 0;
}

Related

access violation writing to 2d array in c

I'm trying to initialise a 2d array, and for some reason I'm getting an exception whenever I try to enter a value into the array.
I'm passing it to the function in a struct, if that makes a difference?
Here's the code:
#define board_size 10
typedef struct {
int b[board_size][board_size];
} board;
board dead_state(); // initialise a dead board state
void main() {
board test_board = dead_state();
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
test_board.b[i][j] = 0;
}
}
}
board dead_state() {
// generate the matrix, set all values to 0, return
board deadboard;
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
deadboard.b[i][j] = 0;
}
}
return deadboard;
}
edit: problem solved, thanks to hyde's comment: i am blind and cannot see, apparently.
You have what looks like a typo.
for (int i = 0; i < board_size - 1; i++) {
for (int j = 0; j < board_size - 1; i++) {
test_board.b[i][j] = 0;
}
}
Both loops increment i, which means i goes out of bounds. You likely meant for the inner loop to increment j, but I don't know why you're only incrementing i and j up to 8, instead of 9.

nested for loop value of not initialized

I know question sounds dumb, I can't really figure out what is wrong in this code?
void sort(int *arr, int size)
{
int min = 0;
for (int i = 0; i < size - 1; i++)
{
for (int j = i; i < size; j++)
{
if (arr[min] > arr[j])
{
min = j;
}
}
if (min != i)
{
Swap(&arr[i], &arr[min]);
}
}
}
The following code should sort the arr but it is giving segmentation fault.
I ran this code via debugger and it says the value of j at line
for (int j = i; i < size; j++)
something like 3234 (not initialized) and program ends. But j should be 0.
debuger screenshort
In your second for loop, it should be j < size, not i < size.
There are 3 problems in your sort function:
The test in the inner for loop uses i instead of j. j is initialized but the test always succeeds and the loop goes on, letting the code access arr beyond its boundaries, causing undefined behavior.
min should initialized to i inside the outer loop,
j should be initialized to i + 1 is the inner loop (minor).
Here is a corrected version:
void sort(int *arr, int size) {
for (int i = 0; i < size - 1; i++) {
int min = i;
for (int j = i + 1; j < size; j++) {
if (arr[min] > arr[j]) {
min = j;
}
}
if (min != i) {
Swap(&arr[i], &arr[min]);
}
}
}

Why am I getting segmentation fault [139]?

The title is pretty clear I think.
I am trying to create a program that calculates a 3x3 linear system using determinants, but I am getting a segmentation fault. Here is the code:
#include<stdio.h>
int determinant(int n, int m, int det[m][n])
{
int res;
res = det[0][0]*det[1][1] - det[0][1]*det[1][0];
return res;
}
int main(void)
{
int arr[3][4], det[2][2], i, j, D; //Dx1, Dx2, Dx3
for(i = 0; i < 3; i++)
{
printf("Eisagete tous suntelestes ths %dhs eksisoshs.", i+1);
scanf("%d %d %d %d", &arr[i][0], &arr[i][1], &arr[i][2], &arr[i][3]);
}
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+1];
}
}
D = arr[0][0]*determinant(2, 2, det);
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+((j == 1) ? 1 : 0)];
}
}
D -= arr[0][1]*determinant(2, 2, det);
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j];
}
}
D += arr[0][2]*determinant(2, 2, det);
printf("%d\n", D);
}
I am getting the error right after completing the first for loop in main.
In the block:
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; i++)
{
det[i][j] = arr[i+1][j+1];
}
}
You increment i in both loops, and adding 1 more to it while reading from the array. So at arr[i+1] you are reading to far.
A segmentation fault basically means you are trying to read something you don't have access to.
You shoud never do what you're doing by passing static array sizes m and n as function argument:
int determinant(int n, int m, int det[m][n])
Check https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html for info

Filling 3d array using for loop in C programming

I'm working on my class project and I'm currently stuck at the most basic one. Basically I have to fill the stack of boxes using loops and 3d array. The stack is 4 width, 4 length and 3 height and I have to fill boxes with 100 items each.
void main(){
int boxShleve[3][4][4];
int i, j, k;
for (i=0; i<3; ++i){
for (j=0; j<4; ++j){
for (k=0; k<4; ++k){
boxShleve[3][4][4] = 100;
}
}
}
printf("%d", boxShleve[3][4][4]);
}
This is the broken piece of my work... How do I make each array has 100 element in it?
This is what you meant to do:
int main()
{
int boxShleve[3][4][4];
int i, j, k;
for (i = 0; i < 3; ++i)
for (j = 0; j < 4; ++j)
for (k = 0; k < 4; ++k)
boxShleve[i][j][k] = 100;
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
for (k = 0; k < 4; k++)
printf("%d ", boxShleve[i][j][k]);
return 0;
}
The reason you need the nested loops is to use the i, j and k as indexes to access the array. So you have to use them.
Same for printing the values.
A faster way to do this if you are using GCC is as follows.
int boxShleve[3][4][4] = {
{[0 ... 2] = 100 },
{[0 ... 3] = 100 },
{[0 ... 3] = 100 } };
#include <stdio.h>
int main(){
int boxShleve[3][4][4];
size_t size = sizeof(boxShleve)/sizeof(int);
int *p = &boxShleve[0][0][0];
while(size--)
*p++ = 100;
printf("%d\n", boxShleve[2][3][3]);//last element,
return 0;
}

2d array filling

I would like to fill 2d array, and this is what I do. However, it would give compile errors such as warning: value computed is not used and i dont understand why. I would appreciate if anyone could show me the problem and explain what could be done. thanks!
#include <stdio.h>
#include <string.h>
int main()
{
int array1[4][4];
int len = sizeof(array1) / sizeof(array1[0]);
int wid = sizeof(array1[0]) / sizeof(array1[0][0]);
int i, j , z = 0;
//compile error
for(i = 0, i < len; i++)
{
for(j = 0, j < wid; j++)
{
array1[i][j] = z;
}
z++;
}
int a, b;
for(a = 0, a < len; a++)
{
for(b = 0, b < wid; b++)
{
printf("%d", array1[a][b]);
}
}
return 0;
}
You have put a comma after the initialization part of each of your for statements. You should put a semicolon. For example, you wrote this:
for(i = 0, i < len; i++)
You need to change it to this:
for(i = 0; i < len; i++)
Also, you will probably want to print spaces between array elements, and a newline after each row:
for(a = 0; a < len; a++) {
for(b = 0; b < wid; b++) {
printf("%d ", array1[a][b]);
}
printf("\n");
}

Resources