char **array;
char *x_ptr = &array[0][0];
int rowcount = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (j == 0) {
rowcount += 1;
*(x_ptr +( i*column + j)) = rowcount+'0';
}
else {
*(x_ptr +( i*column + j)) = 0;
}
}
}
When running this loop for the for the 10th time, why does it store the int value for 10 as symbol:
Current result
8,9,:,;,<,=,>
The ASCII for '0' is 48. If you add 10 to it, you'll get 58, which is the ASCII for ':'.
You should use char array[10][10]; not char **array;
':' == '9' + 1
The following code could work:
#include <stdio.h>
int main()
{
int row = 10, column = 10;
char array[10][10];
int rowcount = 0;
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j)
if (j == 0)
array[i][j] = ++rowcount + '0';
else
array[i][j] = 0;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j)
printf("%c\t", array[i][j]);
printf("\n");
}
return 0;
}
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 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.
#include <stdio.h>
#include <string.h>
int main(void)
{
char names[10][20] = { "kim", "lee", "sin", "jo", "kim2", "chae", "jin", "bak", "so", "choi" };
int i, j;
char tmp[20];
printf("\nfirst\n");
for (j = 0; j < 10; j++)
printf("%5s", names[j]);
printf("\n");
for (i = 0; i < 9; i++)
for (j = 0; j < 9 - i; j++)
if (strcmp(names[j], names[j + 1]) > 0)
{
//tmp = names[j];//
//names[j] = names[j + 1];//
//names[j + 1] = tmp;//
//break;//
//This part.//
}
printf("\nSorted Result\n");
for (j = 0; j < 10; j++)
printf("%5s", names[j]);
printf("\n");
return 0;
}
I have to make this code without strcpy. but this code has l-value error. Any hints or suggestions to this problem?
Instead of declaring tmp and names as array, use pointer, also because of break in for loop, it is not sorting all variables correctly.
Try with below program:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *names[10] = { "kim", "lee", "sin", "jo", "kim2", "chae", "jin", "bak", "so", "choi" };
int i, j;
char *tmp;
printf("\nfirst\n");
for (j = 0; j < 10; j++)
printf("%5s", names[j]);
printf("\n");
for (i = 0; i < 10; i++)
for (j = 0; j < 9 - i; j++)
if (strcmp(names[j], names[j + 1]) > 0)
{
tmp = names[j];
names[j] = names[j + 1];
names[j + 1] = tmp;
}
printf("\nSorted Result\n");
for (j = 0; j < 10; j++)
printf("%5s", names[j]);
printf("\n");
return 0;
}
Here is my code:
int main() {
int x, y;
int *xptr, *yptr;
int array[10][10];
int j;
int k;
int z = 0;
for(j = 0; j < 10; j++) {
for(k = 0; k < 10; k++) {
array[j][k] = j * 10 + k;
}
}
xptr = &array[0][0];
for(j = 0; j < 10; j++) {
for(k = 0; k < 10; k++) {
printf("array[%d][%d] = %d \n", j, k, *(xptr + j), (xptr + k));
}
}
system("PAUSE");
}
I am trying to initialize a 2d array so that at [0][0] it equals 0 and at [9][9] it equals 99. With the way that it is now, [0][0-9] all equal 0 and then [1][0-9] all equal 1. How would I properly load this array in the fashion that I mentioned?
for(j = 0; j < 10; j++) {
for(k = 0; k < 10; k++) {
array[j][k] = j*10 + k;
}
}
I'm assuming you've actually declared everything, but didn't include it in the example. You simply want
array[j][k] = j*10 + k;