c - Placing an integer unto a two dimension array (matrix) -
ok, guys i'm trying make program in guess inputted number placed randomly unto matrix or 2 dimensional array. example user inputted '3' , integer placed on random row , column of matrix.
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define r 4 #define c 4 void display(char a[][c]); void placing(char b[][c]); void guess(int m, int n); void main(){ int x=0,y=0,m,n,number; int matrix[r][c]; char a[r][c]={{'x','x','x','x'},{'x','x','x','x'},{'x','x','x','x'},{'x','x','x','x'}}; printf("\t\t\t welcome game"); printf("\nenter number: "); scanf("%d",&number); display(a); placing(a); printf("\n\nyour number being placed @ random location"); printf("\n\nguess number located (row) (column)"); printf("\n\nenter coordinates: "); scanf("%d%d",&m,&n); getch(); } void display(char a[][c]){ for(int row=0; row<r; row++) { printf ("\n"); for(int column=0; column<c; column++) { printf ("\t%c\t", a[row][column]); } } } void placing(int b[][c]){ int randomrow=0,randomcolumn=0; int i=0; randomrow = rand()%4; randomcolumn = rand()%4; for(int row=0; row < randomrow; row++){ for(int column=0;column < randomcolumn; column++, i++){ b[row][column]; } } printf("%d",b[r][c]); }
this whole program. tips?
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int matrix[4][4] = { {-1,-1,-1,-1}, {-1,-1,-1,-1}, {-1,-1,-1,-1}, {-1,-1,-1,-1}, }; int user_input; int random_row; int random_column; int row_guess; int col_guess; printf("enter number add: \n"); scanf("%d", &user_input); printf("user entered: %d\n", user_input); srand(time(null)); random_row = rand() % 4; random_column = rand() % 4; printf("random_row: %d\n", random_row); printf("random_column: %d\n", random_column); matrix[random_row][random_column] = user_input; for(int row = 0; row < 4; row++) { for(int column = 0; column < 4; column++) { printf("%d ", matrix[row][column]); } printf("\n"); } printf("guess random row: \n"); scanf("%d", &row_guess); printf("user row guess: %d\n", row_guess); printf("guess random column: \n"); scanf("%d", &col_guess); printf("user column guess: %d\n", col_guess); if (matrix[row_guess][col_guess] == user_input) { printf("you guessed correctly!\n"); } else { printf("you guessed incorrectly\n"); } return 0; }
Comments
Post a Comment