The program is suppose to print a 16 by 16 grid and insert multiple elements horizontally at random within the grid it works fine when i try inserting a single element in the grid but when I try multiple it inserts symbols into the grid. I Would like to now why this occurs. Here's a copy of my code so far :
#include <stdio.h>
#include <stdlib.h>
char **create2DArray();
#define WIDTH 16
#define HEIGHT 16
char** myArray; //global array
void main()
{
myArray = create2DArray();
}
char **create2DArray(){
int i,j;
char **array = (char **) malloc(sizeof(char *) * WIDTH);
for(i=0; i<WIDTH; i++)
array[i] = (char *) malloc(sizeof(char) * HEIGHT);
void insertHorizontally(char* word, char** array);
srand(time(NULL));
int WIDTH_1 = (rand()%15)+ 1;
int HEIGHT_1 = (rand()%15) + 1;
int insert;
char* word[] = {"CAT","DOG",};
insert = strlen(word);
for(i=0; i<WIDTH; i++){
for(j=0; j<HEIGHT; j++){
array[i][j] = '.';
}
}
for (j = HEIGHT_1; j < HEIGHT_1 + insert; j ++) {
array[WIDTH_1][j] = word[j - HEIGHT_1];
}
void printArray(char** array);
for(i=0; i<WIDTH; i++){
for(j=0; j<HEIGHT; j++){
printf("%c ", array[i][j]);
}
printf("\n");
}
return array;
}
Any help,tips or advice would be appreciated :)
I hope this help you. It has compiled in Linux. You should try the more readable source for receiving good help here.
Note: If the random function gives the last index of columns in a row, it is possible that your element inserted partially.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **create2DArray();
#define WIDTH 16
#define HEIGHT 16
char** myArray; //global array
char **create2DArray(){
int i,j;
char **array = (char **) malloc(sizeof(char *) * WIDTH);
for(i=0; i<WIDTH; i++)
array[i] = (char *) malloc(sizeof(char) * HEIGHT);
return array;
}
void insertHorizontally(char** word, int num, char** array)
{
srand(time(NULL));
int i, j, k;
int WIDTH_1;
int HEIGHT_1;
int insert;
for(i=0; i<WIDTH; i++){
for(j=0; j<HEIGHT; j++){
array[i][j] = '.';
}
}
for(k = 0; k < num; k++){
insert = strlen(word[k]);
WIDTH_1 = (rand() % WIDTH);
HEIGHT_1 = (rand() % HEIGHT);
for (i= 0, j = HEIGHT_1; i < insert && j < HEIGHT ; j ++, i ++)
array[WIDTH_1][j] = word[k][j - HEIGHT_1];
}
}
void printArray(char** array){
int i, j;
for(i=0; i<WIDTH; i++){
for(j=0; j<HEIGHT; j++){
printf("%c ", array[i][j]);
}
printf("\n");
}
}
void main()
{
myArray = create2DArray();
char *word[] = {{"CAT"},{"DOG"},{"FOX"}};
insertHorizontally(word, sizeof(word)/sizeof(char *), myArray);
printArray(myArray);
}
Related
I'm trying to write a code in C that sum two 4x4 matrix.
But I want my function to have a pointer as my arguments. The only error I'm getting is the time I'm trying to sum up in the function. Could someone help me?
#include <stdio.h>
#include <locale.h>
int i = 0, j = 0;
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
mat_C[i][j] = mat_A[i][j] + mat_B[i][j];
printf("%d", mat_C[i][j]);
}
}
}
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[4][4], mB[4][4], mC[4][4];
int *mat_A, *mat_B, *mat_C;
for(i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Type in the value for Matrix A [%d][%d]: ", i, j);
scanf_s("%d", &mA[i][j]);
}
}
i, j = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Type in the value for Matrix B [%d][%d]: ", i, j);
scanf_s("%d", &mB[i][j]);
}
}
*mat_A = &mA;
*mat_B = &mB;
return 0;
}
The types of pointers for the arguments are wrong. You want to pass (the pointer to the first elements of) arrays like int mA[4][4];, so they should be pointers to int[4].
void calc_soma(int (*mat_A)[4], int (*mat_B)[4], int (*mat_C)[4])
{
/* same as original */
}
They can simply be written like this:
void calc_soma(int mat_A[][4], int mat_B[][4], int mat_C[][4])
{
/* same as original */
}
Then the function can be called like:
calc_soma(mA, mB, mC);
The purpose of mat_A and mat_B are unclear, but if you want to get pointers to the matrice like &mA, it should be int(*)[4][4]. Note that dereferencing (like *mat_A) uninitialized pointers will invoke undefined behavior.
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[4][4], mB[4][4], mC[4][4];
int (*mat_A)[4][4], (*mat_B)[4][4], (*mat_C)[4][4];
/* omit */
mat_A = &mA;
mat_B = &mB;
return 0;
}
To use functions like
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
you should express the matrice by 1D array to match with the format. It will be like this:
#include <stdio.h>
#include <locale.h>
#define ROWS 4
#define COLS 4
int i = 0, j = 0;
void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
mat_C[i * COLS + j] = mat_A[i * COLS + j] + mat_B[i * COLS + j];
printf("%d", mat_C[i * COLS + j]);
}
}
}
int main()
{
setlocale(LC_ALL, "Portuguese");
int i=0, j=0;
int mA[ROWS * COLS], mB[ROWS * COLS], mC[ROWS * COLS];
for(i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("Type in the value for Matrix A [%d][%d]: ", i, j);
scanf_s("%d", &mA[i * COLS + j]);
}
}
i, j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("Type in the value for Matrix B [%d][%d]: ", i, j);
scanf_s("%d", &mB[i * COLS + j]);
}
}
calc_soma(mA, mB, mC);
return 0;
}
I am learning C and am having trouble passing the pointer of a 2D array to another function that then prints the 2D array. Any help would be appreciated.
int main( void ){
char array[50][50];
int SIZE;
...call function to fill array... this part works.
printarray( array, SIZE );
}
void printarray( char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
char ** doesn't represent a 2D array - it would be an array of pointers to pointers. You need to change the definition of printarray if you want to pass it a 2D array:
void printarray( char (*array)[50], int SIZE )
or equivalently:
void printarray( char array[][50], int SIZE )
In main(), the variable "array" is declared as
char array[50][50];
This is a 2500 byte piece of data. When main()'s "array" is passed about, it is a pointer to the beginning of that data. It is a pointer to a char expected to be organized in rows of 50.
Yet in function printarray(), you declare
char **array
"array" here is a pointer to a char *pointer.
#Lucus suggestion of void printarray( char array[][50], int SIZE ) works, except that it is not generic in that your SIZE parameter must be 50.
Idea:
defeat (yeech) the type of parameter array in printarray()
void printarray(void *array, int SIZE ){
int i;
int j;
char *charArray = (char *) array;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", charArray[j*SIZE + i] );
}
printf( "\n" );
}
}
A more elegant solution is to make the "array" in main() an array of pointers.
// Your original printarray()
void printarray(char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
// main()
char **array;
int SIZE;
// Initialization of SIZE is not shown, but let's assume SIZE = 50;
// Allocate table
array = (char **) malloc(SIZE * sizeof(char*));
// Note: cleaner alternative syntax
// array = malloc(sizeof *array * SIZE);
// Allocate rows
for (int row = 0; row<SIZE; row++) {
// Note: sizeof(char) is 1. (#Carl Norum)
// Shown here to help show difference between this malloc() and the above one.
array[row] = (char *) malloc(SIZE * sizeof(char));
// Note: cleaner alternative syntax
// array[row] = malloc(sizeof(**array) * SIZE);
}
// Initialize each element.
for (int row = 0; row<SIZE; row++) {
for (int col = 0; col<SIZE; col++) {
array[row][col] = 'a'; // or whatever value you want
}
}
// Print it
printarray(array, SIZE);
...
Since C99 supports dynamic-sized arrays, the following style is simply more convenient to pass a 2-dim array:
void printarray( void *array0, int SIZE ){
char (*array)[SIZE] = array0;
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
none of the answers here were what I was looking for, so I'm posting my simple solution to the problem
#include <iostream>
using namespace std;
void example(int* mat, int dim0, int dim1){
for(int i = 0; i < dim0; ++i) {
for(int j = 0; j < dim1; ++j) {
auto cur_index = i * dim1 + j;
cout<< *(mat + cur_index) << endl;
}
}
}
int main()
{
const int dim0 = 3;
const int dim1 = 2;
int mat[dim0][dim1];
for(int i = 0; i < dim0; ++i) {
for(int j = 0; j < dim1; ++j) {
mat[i][j] = i * dim1 + j;
}
}
example(&(mat[0][0]), dim0, dim1);
return 0;
}
You can easily pass the 2d array using double pointer.
void printarray( char **array, int n)
{
int i, j;
for(i=0; i<n; i++ )
{
for(j=0; j<n; j++)
{
printf("%c ", array[i][j] );
}
printf( "\n" );
}
}
int main()
{
int n = 2;
int i, j;
char **array = (char **) malloc(n * sizeof(char*));
for (i=0; i<n; i++)
{
array[i] = (char *) malloc(n* sizeof(char));
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
scanf("%c ", &array[i][j]);
}
}
printarray(array, n);
return 0;
}
Full Code : Ideone
I am learning C and am having trouble passing the pointer of a 2D array to another function that then prints the 2D array. Any help would be appreciated.
int main( void ){
char array[50][50];
int SIZE;
...call function to fill array... this part works.
printarray( array, SIZE );
}
void printarray( char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
char ** doesn't represent a 2D array - it would be an array of pointers to pointers. You need to change the definition of printarray if you want to pass it a 2D array:
void printarray( char (*array)[50], int SIZE )
or equivalently:
void printarray( char array[][50], int SIZE )
In main(), the variable "array" is declared as
char array[50][50];
This is a 2500 byte piece of data. When main()'s "array" is passed about, it is a pointer to the beginning of that data. It is a pointer to a char expected to be organized in rows of 50.
Yet in function printarray(), you declare
char **array
"array" here is a pointer to a char *pointer.
#Lucus suggestion of void printarray( char array[][50], int SIZE ) works, except that it is not generic in that your SIZE parameter must be 50.
Idea:
defeat (yeech) the type of parameter array in printarray()
void printarray(void *array, int SIZE ){
int i;
int j;
char *charArray = (char *) array;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", charArray[j*SIZE + i] );
}
printf( "\n" );
}
}
A more elegant solution is to make the "array" in main() an array of pointers.
// Your original printarray()
void printarray(char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
// main()
char **array;
int SIZE;
// Initialization of SIZE is not shown, but let's assume SIZE = 50;
// Allocate table
array = (char **) malloc(SIZE * sizeof(char*));
// Note: cleaner alternative syntax
// array = malloc(sizeof *array * SIZE);
// Allocate rows
for (int row = 0; row<SIZE; row++) {
// Note: sizeof(char) is 1. (#Carl Norum)
// Shown here to help show difference between this malloc() and the above one.
array[row] = (char *) malloc(SIZE * sizeof(char));
// Note: cleaner alternative syntax
// array[row] = malloc(sizeof(**array) * SIZE);
}
// Initialize each element.
for (int row = 0; row<SIZE; row++) {
for (int col = 0; col<SIZE; col++) {
array[row][col] = 'a'; // or whatever value you want
}
}
// Print it
printarray(array, SIZE);
...
Since C99 supports dynamic-sized arrays, the following style is simply more convenient to pass a 2-dim array:
void printarray( void *array0, int SIZE ){
char (*array)[SIZE] = array0;
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
none of the answers here were what I was looking for, so I'm posting my simple solution to the problem
#include <iostream>
using namespace std;
void example(int* mat, int dim0, int dim1){
for(int i = 0; i < dim0; ++i) {
for(int j = 0; j < dim1; ++j) {
auto cur_index = i * dim1 + j;
cout<< *(mat + cur_index) << endl;
}
}
}
int main()
{
const int dim0 = 3;
const int dim1 = 2;
int mat[dim0][dim1];
for(int i = 0; i < dim0; ++i) {
for(int j = 0; j < dim1; ++j) {
mat[i][j] = i * dim1 + j;
}
}
example(&(mat[0][0]), dim0, dim1);
return 0;
}
You can easily pass the 2d array using double pointer.
void printarray( char **array, int n)
{
int i, j;
for(i=0; i<n; i++ )
{
for(j=0; j<n; j++)
{
printf("%c ", array[i][j] );
}
printf( "\n" );
}
}
int main()
{
int n = 2;
int i, j;
char **array = (char **) malloc(n * sizeof(char*));
for (i=0; i<n; i++)
{
array[i] = (char *) malloc(n* sizeof(char));
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
scanf("%c ", &array[i][j]);
}
}
printarray(array, n);
return 0;
}
Full Code : Ideone
I am trying to send a pointer of a matrix to function for printing the values. However, my following code prints some long numbers so I assumed it prints the addresses instead! How can I print the value of the matrix after passing the pointer of it?
#include <stdio.h>
#include <string.h>
#include <math.h>
void printing(int *edge);
void main(){
int N=3;
int i,j;
int *edge[N];
for (i = 0; i < N; i++){
*(edge+i) = (int *)malloc(N * sizeof(int));
}
srand(0);
for(i = 0; i < N; i++){
for(j = 0; j < N; j++){
if(i == j)
*(*(edge+i)+j) = 0;
else
*(*(edge+i)+j) = 1; //rand() % 10;
}
}
printing(edge); // Pass the pointer of the matrix
}
void printing(int *edge){
int i,j;
int N= 3;
for( i = 0; i < N; i++){
for(j = 0; j < N; j++){
printf("%d \t", ((edge+i)+j)); // I think I have to do something in this line.
}
printf("\n");
}
}
The parameter type of printing is incorrect. It should be int *edge[]. Then when you print, use *(*(edge+i)+j), or better yet edge[i][j].
The result:
void printing(int *edge[]){
int i,j;
int N = 3;
for( i = 0; i < N; i++){
for(j = 0; j < N; j++){
printf("%d \t", edge[i][j]);
}
printf("\n");
}
}
Also, be sure to #include <stdlib.h>, as it's needed for malloc and srand.
I am learning C and am having trouble passing the pointer of a 2D array to another function that then prints the 2D array. Any help would be appreciated.
int main( void ){
char array[50][50];
int SIZE;
...call function to fill array... this part works.
printarray( array, SIZE );
}
void printarray( char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
char ** doesn't represent a 2D array - it would be an array of pointers to pointers. You need to change the definition of printarray if you want to pass it a 2D array:
void printarray( char (*array)[50], int SIZE )
or equivalently:
void printarray( char array[][50], int SIZE )
In main(), the variable "array" is declared as
char array[50][50];
This is a 2500 byte piece of data. When main()'s "array" is passed about, it is a pointer to the beginning of that data. It is a pointer to a char expected to be organized in rows of 50.
Yet in function printarray(), you declare
char **array
"array" here is a pointer to a char *pointer.
#Lucus suggestion of void printarray( char array[][50], int SIZE ) works, except that it is not generic in that your SIZE parameter must be 50.
Idea:
defeat (yeech) the type of parameter array in printarray()
void printarray(void *array, int SIZE ){
int i;
int j;
char *charArray = (char *) array;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", charArray[j*SIZE + i] );
}
printf( "\n" );
}
}
A more elegant solution is to make the "array" in main() an array of pointers.
// Your original printarray()
void printarray(char **array, int SIZE ){
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
// main()
char **array;
int SIZE;
// Initialization of SIZE is not shown, but let's assume SIZE = 50;
// Allocate table
array = (char **) malloc(SIZE * sizeof(char*));
// Note: cleaner alternative syntax
// array = malloc(sizeof *array * SIZE);
// Allocate rows
for (int row = 0; row<SIZE; row++) {
// Note: sizeof(char) is 1. (#Carl Norum)
// Shown here to help show difference between this malloc() and the above one.
array[row] = (char *) malloc(SIZE * sizeof(char));
// Note: cleaner alternative syntax
// array[row] = malloc(sizeof(**array) * SIZE);
}
// Initialize each element.
for (int row = 0; row<SIZE; row++) {
for (int col = 0; col<SIZE; col++) {
array[row][col] = 'a'; // or whatever value you want
}
}
// Print it
printarray(array, SIZE);
...
Since C99 supports dynamic-sized arrays, the following style is simply more convenient to pass a 2-dim array:
void printarray( void *array0, int SIZE ){
char (*array)[SIZE] = array0;
int i;
int j;
for( j = 0; j < SIZE; j++ ){
for( i = 0; i < SIZE; i ++){
printf( "%c ", array[j][i] );
}
printf( "\n" );
}
}
none of the answers here were what I was looking for, so I'm posting my simple solution to the problem
#include <iostream>
using namespace std;
void example(int* mat, int dim0, int dim1){
for(int i = 0; i < dim0; ++i) {
for(int j = 0; j < dim1; ++j) {
auto cur_index = i * dim1 + j;
cout<< *(mat + cur_index) << endl;
}
}
}
int main()
{
const int dim0 = 3;
const int dim1 = 2;
int mat[dim0][dim1];
for(int i = 0; i < dim0; ++i) {
for(int j = 0; j < dim1; ++j) {
mat[i][j] = i * dim1 + j;
}
}
example(&(mat[0][0]), dim0, dim1);
return 0;
}
You can easily pass the 2d array using double pointer.
void printarray( char **array, int n)
{
int i, j;
for(i=0; i<n; i++ )
{
for(j=0; j<n; j++)
{
printf("%c ", array[i][j] );
}
printf( "\n" );
}
}
int main()
{
int n = 2;
int i, j;
char **array = (char **) malloc(n * sizeof(char*));
for (i=0; i<n; i++)
{
array[i] = (char *) malloc(n* sizeof(char));
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
scanf("%c ", &array[i][j]);
}
}
printarray(array, n);
return 0;
}
Full Code : Ideone