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.
Related
#include <stdio.h>
#include <stdlib.h>
void addition(int column, int row) {
int **array_a = (int **)malloc(sizeof(int*) * column);
int **array_b = (int **)malloc(sizeof(int*) * column);
int **array_c = (int **)malloc(sizeof(int*) * column);
for (int i = 0; i < column; i++) {
array_a[i] = (int *)malloc(sizeof(int) * row);
}
for (int i = 0; i < column; i++) {
for (int k = 0; k < row; k++) {
array_a[i][k] = rand();
}
for (int i = 0; i < column;) {
array_b[i] = (int *)malloc(sizeof(int) * row);
}
for (int i = 0; i < column; i++) {
for (int k = 0; k < row; k++) {
array_b[i][k] = rand();
}
}
for (int i = 0; i < column;) {
array_c[i] = (int *)malloc(sizeof(int) * row);
}
for (int i = 0; i < column; i++) {
for (int k = 0; k < row; k++) {
array_c[i][k] = array_a[i][k] + array_b[i][k];
}
}
for (int i = 0; i < column; i++) {
for (int k = 0; k < row; k++) {
printf("%d ", array_c[i][k]);
}
free(array_a);
free(array_b);
free(array_c);
}
}
}
int main(void) {
int column, row;
scanf("%d" "%d", &column, &row);
addition(column, row);
return 0;
}
I'm building a function that creates two 2D arrays and fills the values with random numbers and combines their values.
There is no error but the printf isn't working. Any way to make it work?
I don't know how to explain in more details but if you ask me I'll answer.
There are multiple problems in your code:
the loops are not properly nested: the body of the second for (int i = 0; i < column; i++) loop should stop right after the nested for for (int k = 0; k < row; k++) loop.
there are missing i++ increments in some of the loops: for (int i = 0; i < column;) should be
for (int i = 0; i < column; i++)
This is the main issue as it causes an infinite loop, repeatedly initializing array_b[0] with a newly allocated block, until memory is exhausted and beyond.
you do not check for memory allocation failure.
your use or columns and rows is odd: the first index in a 2D usually refers to rows and the second to columns, but since your usage is consistent, it is probably not a problem.
you do not output a newline after each row or column: all output is produced on a single line without a trailing newline. This may cause your output to not appear on some systems. You might want to pause the program after the output to let the user see the output.
casting the return value of malloc() is not necessary in C.
you do not free the subarrays.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
int **allocate_array(int column, int row) {
int **array = malloc(sizeof(array[0]) * column);
if (array == NULL)
return NULL;
for (int i = 0; i < column; i++) {
array_a[i] = malloc(sizeof(array[i][0]) * row);
if (array_a[i] == NULL) {
while (i-- > 0) {
free(array[i]);
}
free(array);
return NULL;
}
}
return array;
}
void free_array(int **array, int column, int row) {
if (array) {
for (int i = 0; i < column; i++) {
free(array[i]);
}
free(array);
}
}
void addition(int column, int row) {
int **array_a = allocate_array(column, row);
int **array_b = allocate_array(column, row);
int **array_c = allocate_array(column, row);
if (array_a && array_b && array_c) {
for (int i = 0; i < column; i++) {
for (int k = 0; k < row; k++) {
array_a[i][k] = rand();
}
}
for (int i = 0; i < column; i++) {
for (int k = 0; k < row; k++) {
array_b[i][k] = rand();
}
}
for (int i = 0; i < column; i++) {
for (int k = 0; k < row; k++) {
array_c[i][k] = array_a[i][k] + array_b[i][k];
}
}
for (int i = 0; i < column; i++) {
for (int k = 0; k < row; k++) {
printf("%d ", array_c[i][k]);
}
printf("\n");
}
}
free_array(array_a, column, row);
free_array(array_b, column, row);
free_array(array_c, column, row);
}
int main(void) {
int column, row;
if (scanf("%d%d", &column, &row) == 2 && column > 0 && row > 0)
addition(column, row);
return 0;
}
As the title says, I just want to paste this input into the program and then every number should be stored in 2D array.
Input:
172549683
645873219
389261745
496327851
813456972
257198436
964715328
731682594
528934167
My attempt:
#include <stdio.h>
int main() {
int array_2d[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
scanf_s(" %d", &array_2d[i][j]);
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
printf("%d", array_2d[i][j]);
}
printf("\n");
}
}
This example shows how read whole number from a row and then split it into its digits that will be save into a 2D matrix.
#include <stdio.h>
int main() {
int array_2d[9][9];
int value = 0;
for (int i = 0; i < 9; i++) {
scanf("%d", &value);
int j = 8;
while (value != 0) {
array_2d[i][j--] = value % 10;
value = value / 10;
}
}
printf("\n\n");
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
printf("[%d]", array_2d[i][j]);
}
printf("\n");
}
}
I have taken two 2d arrays but the output is very different from expected.It should merge two 2d arrays into one 1d array.I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? arraySum[i][j] = array2[i][j] : arraySum[i][j] = array1[i-3][j];
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I want to merge two arrays.Not add.Everywhere the information and examples are about merging two 1D arrays.Please help someone.I don't know where I am wrong.
Please try this code,To Merge two 2d arrays into one 1d array in C
#include <stdio.h>
void print(int a[][3],int m);
int main()
{
int array1[3][3];
int array2[3][3];
int arraySum[6][3];
int k = 0; //put into array;
int l = 10;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
array1[i][j] = ++k; //fill from 1 to 10
array2[i][j] = ++l; //fill from 11 - 19
}
}
/*merge arrays*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
(i < 3) ? (arraySum[i][j] = array2[i][j]) : (arraySum[i][j] = array1[i-3][j]);
//fill arraySum with array2 and append with array1.
//just so that arraySum does not have any order
}
}
printf("Arrays before sorting");
printf("Array 1: ");
print(array1,3);
printf("Array2: ");
print(array2,3);
printf("arraySum");
print(arraySum,6);
/* bubble sort*/
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 3; j++)
{
for(int k = i+1; k < 6; k++)
{
for(int m = 0; m < 3; m++)
{
if(arraySum[i][j] > arraySum[k][m])
{
//swap
int temp = arraySum[i][j];
arraySum[i][j] = arraySum[k][m];
arraySum[k][m] = temp;
}
}
}
}
}
printf("\n\n Merged Array after sorting");
print(arraySum,6);
return 0;
}
void print(int a[][3],int m)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < 3; j++)
{
printf("%d" , a[i][j]);
}
}
}
I hope this code will be usefull.
Thank you.
I believe you're trying to be too smart with ternary operator, you can do it simpler way:
if (i < 3)
arraySum[i][j] = array2[i][j];
else
arraySum[i][j] = array1[i-3][j];
Listen to your compiler it would've told you what was wrong if you've compiled with -Wall -Wextra.
And if you insist on using ternary then this would probably be clearer:
arraySum[i][j] = (i < 3) ? array2[i][j] : array1[i-3][j];
Hi I am working on a C program that declares a 2 dimensional integer array with N rows and M columns and loads it with consecutive numbers using conventional array element access methods. N and M are equal to 10. I need display a specific row of the array using this function
void showRow(int *arrayName, int rowNumber, int nColsInRow)
Then I need to use that function to display the entire array.
I have to do the same thing with the showCol function in my code below.
I have both functions in my code, but I am getting confused on how to implement them. I appreciate any help that y'all can give.
int main(void){
int array[10][10];
int i,j;
int *row0, *row1, *row2, *row3, *row4, *row5, *temp;
int add = 0;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
array[i][j]= add;
add++;
}
}
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
printf("%d ", array[i][j]);
}
printf(" \n");
}
void showRow(int *array, int rowNumber, int nColsInRow)
{
row0 = &(array[0][0]);
row1 = &(array[1][0]);
row2 = &(array[2][0]);
row3 = &(array[3][0]);
row4 = &(array[4][0]);
row5 = &(array[5][0]);
row6 = &(array[6][0]);
row7 = &(array[7][0]);
row8 = &(array[8][0]);
row9 = &(array[9][0]);
row10 = &(array[10][0]);
for(j = 0; j < 10; j++)
{
printf(" %d", *(rowNumber + j);
}
//}
void showCol(int *array, int colNumber, int nRowsInCol)
{
col0 = &(array[0][0]);
col1 = &(array[0][1]);
col2 = &(array[0][2]);
col3 = &(array[0][3]);
col4 = &(array[0][4]);
col5 = &(array[0][5]);
col6 = &(array[0][6]);
col7 = &(array[0][7]);
col8 = &(array[0][8]);
col9 = &(array[0][9]);
col10 = &(array[0][10]);
for(j = 0; j < 10; j++)
{
printf(" %d", *(col + j);
}
}
}
I made a change to the showRow function to accept a void pointer. Then inside of the showRow function I cast it back to 2 dim array. After that, you can go back to treating just like you would any other 2 dim array.
void showRow(void *array, int rowNumber, int nColsInRow)
{
int (*a)[10] = (int (*)[10]) array;
int i;
for (i=0; i < nColsInRow; i++) {
printf("%d\n", a[rowNumber][i]);
}
}
int main()
{
int array[10][10];
int i,j;
int add = 0;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
array[i][j]= add;
add++;
}
}
showRow (&array, 1, 10);
return 0;
}
Without changing the signature of the functions, try this code:
#include <stdio.h>
#include <stdlib.h>
void showRow(int *array, int rowNumber, int nColsInRow);
void showCol(int *array, int colNumber, int nRowsInCol);
int
main(void)
{
int array[10][10];
int i,j;
int *p;
int add = 0;
p = (int *)array; //Set a pointer point to this array.
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
array[i][j]= add;
add++;
}
}
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
printf("%d ", array[i][j]);
}
printf(" \n");
}
showRow( p, 2, 3);
showCol( p, 2, 3);
}
void showRow(int *array, int rowNumber, int nColsInRow)
{
int *row = &array[rowNumber*10];//row points at the particular row.
int j;
for(j = 0; j < 10; j++)
{
printf(" %d", row[j]);
}
printf("\n%d\n", row[nColsInRow]);
}
void showCol(int *array, int colNumber, int nRowsInCol)
{
int *col = &array[colNumber]; //col points at the particular col of first row.
int j, n;
for(j = 0; j < 100; j = j + 10) //Increment 10 since row size is 10.
{
printf(" %d", col[j]);
}
n = 10 * nRowsInCol;
printf( "\n%d\n", col[n]);
}
Explanation: An integer array is stored in a continuous memory.
In this case, although it is a 2 dimensional array,
it stored in 100 continuous unit of 4 bytes. (assuming
size of int is 4 in your computer.)
With int pointer, it can point to any index.
e.g. int *p = &array[58];
p points at 58th unit of this array.
Using this concept, now you can do math to get
particular row or column.
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