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];
Related
I'm making a program that multiplies 3 matrices and print the outcome. The program can input several cases of multiplication and each case can determined their own number of NxN matrix (n). However, if I input 2 cases, first with n=2 and second with n=3, the output of the first case will have a 3x3 matrix with row 3 and column 3 only have 0s. How do I fix this problem?
#include <stdio.h>
int main(){
int t, n, i, j, k, l, a[50][50][10], b[50][50][10], c[50][50][10], d[50][50][10], e[50][50][10];
scanf ("%d", &t);
for (l=1; l<=t; l++){
scanf("%d", &n);
// matrix a
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j][l]);
}
}
// matrix b
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j][l]);
}
}
// matrix c
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & c[i][j][l]);
}
}
//Multiplication
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
d[i][j][l] = 0;
for (k = 0; k < n; k++) {
d[i][j][l] += a[i][k][l] * b[k][j][l];
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
e[i][j][l] = 0;
for (k = 0; k < n; k++) {
e[i][j][l] += d[i][k][l] * c[k][j][l];
}
}
}
}
//Printing the product
for (l=1; l<=t; l++){
printf ("Case #%d:\n", l);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", e[i][j][l]);
}
printf("\n");
}
}
return 0;
}
This is the expected output.
Write a program which will accept 2-dimensional square matrix and find out the transpose of it.
Program should not make use of another matrix
Hi I am trying to transpose a 2*2 matrix without using another matrix.
Is there anything wrong with my transpose logic?
I am a newbie
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1); //i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) { //loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
EDIT: I found an easier way to do it however I still don't understand why my transpose logic by using this
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
cannot get it to transpose.
Below is my corrected answer
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);//i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) {//loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[j][i]);
}
printf("\n");
}
}
Your program is a good attempt, but transposing the matrix is like reversing an array: you must stop half way to avoid swapping the transposed values twice, leading to the original matrix as you observe.
You should stop the inner loop when j == i, hence change the inner loop to:
for (j = 0; j < i; j++) { // j < i instead of j < 2
Here is a modified version:
#include <stdio.h>
int main() {
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);
for (j = 0; j < 2; j++) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
printf("The matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < i; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
Your corrected answer does not transpose the matrix at all, it merely outputs the transposed matrix. The matrix mat in memory is unchanged.
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.
Edid: Thanks for the suggestions, but none of them work for the FOR loop error.
Why the triple nested for loop(line 82 - 98) works only the first time(correctly) and then it just spits crazy random numbers? The first time it works so good and then it does everything wrong. Thanks for the help!
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
// 2 players, 5 zara, 3 puti(ako ne si gi haresa), moje da pazite:
//1 qift
// 2 qifta
// ful: 1*2 i 1*3
// 5 ednakvi: general
// 4 ednakvi
// 1*3
// 5 poredni
// shans
// izpulnqvase 8 puti i moje da se zapazva zara (samo pri 1 i 2 hvurlane)
int np, otg[5], otg10[5];
int check_pairs(int *otg10, int time){
int dicecopy[5];
memcpy(dicecopy, otg10, sizeof(int) * 5);
for(int k = 0; k < 5; k++){
printf("%d " , dicecopy[k]);
}
int pairs = 0, sum = 0;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
if(dicecopy[i] == dicecopy[j])
pairs++;
}
}
// points
if(pairs == 1*2 || pairs == 2*2){
printf("GGGGGGG");
for(int k = 0; k < 5; k++){
sum += dicecopy[k];
}
} else {
return 0;
}
return sum;
}
int main(){
printf("How many players do you want: ");
scanf("%d" , &np);
int points[np];
for(int i = 0; i < np; i++){
points[i] = 0;
}
int dice[8][np][3][5];
srand(time(NULL));
for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
for(int c = 0; c < 3; c++){
for(int k = 0; k < 5; k++){
dice[i][j][c][k] = rand() % 6 + 1;
}
}
}
}
/* for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
for(int c = 0; c < 3; c++){
for(int k = 0; k < 5; k++){
printf("%d " , dice[i][j][c][k]);
}
putchar('\n');
}
putchar('\n');
}
putchar('\n\n');
}
*/
char anws = 'y';
for(int i = 0; i < 8; i++){
for(int j = 0; j < np; j++){
printf("\nPlayer: %d\nPoints: %d\n" , j + 1 , points[j]);
for(int k = 0; k < 3; k++){
for(int l = 0; l < 5; l++){
otg10[l] = dice[i][j][k][l];
printf("%d " , otg10[l]);
}
printf("\n\nDo you like it?\n");
scanf("%s" , &anws);
if(anws == 'y' || anws == 'Y'){
points[j] = check_pairs(otg10 , k);
break;
}
}
}
}
return 0;
}
The code is still not finished I have to do more work on the other check-ups. BTW this will represent the game called Generala, if you have any suggestions I'd like to hear them!