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.
Related
I want to create the following looking 2D array of "-"
I attempted something like this but it did not work (for 10 by 10)
char table[10][10];
for (int i = 0; i < 10; i ++){
for (int j = 0; j < 10; j++)
{
strcpy(table[10][i], "-");
}
}
The whole 2D array?
If not strings, use memset(table, '-', sizeof table); to fill every byte with '-'. No for loop needed.
char table[10][10];
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i ++){
for (size_t j = 0; j < sizeof(table[i]); j++)
{
table[i][j] = `-`);
}
}
or memset(table, '-', sizeof(table))
If you want to have 10 strings (null character terminated)
for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i ++){
memset(table[i], `-`, sizeof(table[i]) - 1);
table[i][sizeof(table[i]) - 1] = 0;
}
Not advocating this for portability, but if you're using GCC, you can initialize at the declaration using the following GNU extension
char table[10][10] = { [0 ... 9] = {[0 ... 9] = '-'} };
Demo
(blatant ripoff of this answer)
Instead of using strcpy we can assign values like this // table[i][j]= '-'; //
printf("%c",table[i][j]); //It is used to print characters//
#include <stdio.h>
int main()
{
char table[10][10];
for (int i = 0; i < 10; i ++)
{
for (int j = 0; j < 10; j++)
{
table[i][j]= '-'; //instead of using strcpy we can assign values like this//
}
}
for (int i = 0; i < 10; i ++)
{
for (int j = 0; j < 10; j++)
{
printf("%c",table[i][j]); //It is used to print characters//
}
printf("\n"); //It is used to have a new line//
}
return 0;
}
I have a 2D array (big_array) and there are four values inside of it = 1,
I want to get the indexes of these four values and put them in the first index of each array in another 2D array (small_array) one by one.
I tried this:
for (int x = 0; x < 3; x++) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; x++) {
if (big_array[i][j] == 1) {
small_array[x][0] = j;
break;
}
}
}
}
And I get only the index of the last value=1 in all of array of 'small_array'.
I don't get what you are trying to achieve, but If you're trying to store the second index of each element that equals 1 of the big_array in the first column of the small_array, then here is how :
int x = 0;
while (x < 3)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (big_array[i][j] == 1)
{
small_array[x][0] = j;
x++;
}
}
}
}
I know that object oriented is not welcoming in c language but I still trying to work my way because it is possible as later languages based on c works good with objects
so my question is when I try to write a function outside of main it doesnt give me access and doesnt change values of wanted structs , see code below (I marked every things and included working functions inside the main) :
#include <stdio.h>
#include <stdlib.h>
static int RandomNum() {
int generate = rand() / 100000000;
while (generate >= 9) {
generate = rand() / 100000000;
}
return generate;
}
// Object Definition
typedef struct {
int a, b;
int Mat[2][4];
} Matrix2x4;
typedef struct {
int a, b;
int Mat[4][5];
} Matrix4x5;
typedef struct {
int a, b;
int Mat[4][5];
} Matrix2x5;
void PrintMat2x4(Matrix2x4 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
void PrintMat4x5(Matrix4x5 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
void PrintMat2x5(Matrix2x5 a) {
int row = a.a;
int col = a.b;
for (int i = 0; i < row; i++) {
printf("{%s", " ");
for (int j = 0; j < col; j++) {
printf("%d ,", a.Mat[i][j]);
}
printf("}%s\n", " ");
}
}
// NOT WORKING AS A SEPERATE FUNCTION SO I PUT IT INSIDE THE MAIN
/*static void MatrixMultiplication (Matrix2x4 a , Matrix4x5 b , Matrix2x5 c) {
for (int i = 0; i < a.b; i++){
for (int j = 0; j < b.b; j++){
for (int k = 0; k < a.b; k++){
c.Mat[i][j] = c.Mat[i][j]+(a.Mat[i][k]*b.Mat[k][j]);}}}
}
void setRandom (Matrix a) {
int row = ((int)(sizeof (a.Mat) / sizeof (a.Mat)[0])) ;
int col = ((int)(sizeof (a.Mat)[0] / sizeof (a.Mat)[0][0])) ;
for (int i = 0; i < row ; i++){
for (int j = 0; j < col; j++){
a.Mat[i][j] = RandomNum();}}}*/
void main() {
printf("%s\n\n", "Start..");
Matrix2x4 test = {2,4,{0}}; // <----- SIZE IS 2X4
int row = ((int)(sizeof(test.Mat) / sizeof(test.Mat)[0]));
int col = ((int)(sizeof(test.Mat)[0] / sizeof(test.Mat)[0][0]));
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
test.Mat[i][j] = RandomNum();
}
}
Matrix4x5 test2 = {4,5,{0}}; // <----- SIZE IS 4X5
int row2 = ((int)(sizeof(test2.Mat) / sizeof(test2.Mat)[0]));
int col2 = ((int)(sizeof(test2.Mat)[0] / sizeof(test2.Mat)[0][0]));
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
test2.Mat[i][j] = RandomNum();
}
}
Matrix2x5 mult = {2,5,{0}}; // <----- SIZE IS 2X5
PrintMat2x4(test);
printf("X\n");
PrintMat4x5(test2);
printf("=\n");
for (int i = 0; i < test.b; i++) {
for (int j = 0; j < test2.b; j++) {
for (int k = 0; k < test.b; k++) {
mult.Mat[i][j] = mult.Mat[i][j] + (test.Mat[i][k] * test2.Mat[k][j]);
}
}
}
PrintMat2x5(mult);
printf("\n\n%s", "End ---> ");
}
In order for this function to work:
static void MatrixMultiplication (Matrix2x4 a , Matrix4x5 b , Matrix2x5 c) {
for (int i = 0; i < a.b; i++){
for (int j = 0; j < b.b; j++){
for (int k = 0; k < a.b; k++){
c.Mat[i][j] = c.Mat[i][j]+(a.Mat[i][k]*b.Mat[k][j]);}}}
}
You must pass your structs as pointers:
static void MatrixMultiplication (Matrix2x4* a , Matrix4x5* b , Matrix2x5* c) {
for (int i = 0; i < a->b; i++){
for (int j = 0; j < b->b; j++){
for (int k = 0; k < a->b; k++){
c->Mat[i][j] = c->Mat[i][j]+(a->Mat[i][k]*b->Mat[k][j]);}}}
}
Your original function doesn't work outside of main, because C is making a copy of those structs for the function's own use. Therefore the original structs never get modified. Changing those parameters to pointers means that the function is working with the original structs.
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]);
}
}
}
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;
}