I want to pass two matrices as argument. These matrices have different size and i don't understand how i have to do this work:
#include <stdio.h>
#include <stdlib.h>
void f(int m[3][], int n);
int main()
{
int A[3][3]={{1,2,3},{4,5, 6},{7,8,9}};
int B[3][2]={{1,2},{3, 4}, {5, 6}};
f(A, 3);
f(B, 2);
return 0;
}
void f(int m[3][], int n)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<n;j++)
printf("%5d", m[i][j]);
}
return;
}
How can I do this?
The only safe way that I know of to do this is to include the matrix dimensions in the parameters, or make some kind of matrix struct
Option A) dimensions as parameters
void f(int **m, int w, int h )
{
int i,j;
for(i=0;i<w;i++)
{
for(j=0;j<h;j++)
printf("%5d", m[i][j]);
}
return;
}
Option B) Use a struct
typedef struct Matrix
{
int w, h;
int** m;
} Matrix;
void f ( Matrix *m )
{
for ( int i = 0; i < m->w; ++i )
{
for ( int j = 0; j < m->h; ++j )
{
printf(%5d", m->m[i][j]);
}
}
}
Related
I want to output the median of the array using pointer functions, here is my code:
#include <stdio.h>
void swap(int *a,int *b) {
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void BubbleSort(int n, int arr[]) //passing reference
{
int i, j;
for(i = 0; n - 1 > i; i++) // jika array sepanjang 8, proses bubblesort terjadi hanya 7 kali
{
for(j = 0; n - 1 > j; j++)
{
if(arr[j] > arr[j + 1])
{
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int* find_middle(int a[], int n){
int sum,i;
int median;
if(n%2!=0){
median = (n/2)+1;
}
else if(n%2==0){
median = (n+1)/2;
}
return &a[median];
}
int main(){
int naData[10]={0,1,2,3,4,5,6,7,8,9};
int naData2[11]={9,8,7,5,4,3,2,1};
BubbleSort(sizeof(naData),naData);
BubbleSort(sizeof(naData),naData);
int func1 = *find_middle(naData, sizeof(naData));
int func2 = *find_middle(naData2, sizeof(naData2));
printf("%d\n", func1);
printf("%d\n", func2);
return 0;
}
Currently, this function outputs
2
87
Here is what it should output:
5
5
How to fix this problem, while still implementing the pointers?
p.s. If the array has even elements, take the larger element as the median.
p.s. naData2 has the size [11], and it cannot be changed since it is a requirement from the professor
I tried to build a heap and finally print the elements in the form of an array.
Here it is the code (I know this doesn't really make sense but I just wanted to test my knowlwdge of heap and dynamic arrays):
#include <stdio.h>
#include <stdlib.h>
void heapiify(int *arr,int n, int i)
{
int largest=i;
int l=2*i+1; // left node
int r= 2*i+2; // right node
if(l<=n && *arr[l]>=*arr[i])
largest=l;
if (r <=n && *arr[r]<=*arr[i])
largest= r;
if(largest !=i)
{
int temp=*arr[i];
*arr[i]=*arr[largest];
*arr[largest]=temp;
}
heapify(*arr,n,largest);
}
void buildh(int *arr,int n,int r,int c)
{
int i;
for(i=n/2-1;i>=0;i--)
heapify(*arr,n,i);
output(*arr,r,c);
}
void output(int *arr,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d",*arr[i*c+j]);
}
printf("\n");
}
}
int main()
{
int i,j,r,c;
printf("enter the number of rows");
scanf("%d",&r);
printf("enter the number of columns");
scanf("%d",&c);
int n=r*c;
int *arr=malloc(n*sizeof(int));
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
scanf("%d",&arr[i*c+j]);
}
buildh(*arr,n,r,c);
}
I'm getting 9 errors which are all the same
invalid argument type of unary '*'( have int)
Your arr variable is of type pointer to int:
int *arr=malloc(n*sizeof(int));
So when you call buildh, which takes the same type, you have to pass it as-is:
buildh(arr,n,r,c);
Same for the other cases.
The problem is the dereference of arr, across your funtions in multiple places, and the passing of dereferenced *arr in your functions to int * parameters, you should pass arr, try:
//...
void heapify(int *arr, int n, int i)
{
int largest = i;
int l = 2 * i + 1; // left node
int r = 2 * i + 2; // right node
if (l <= n && arr[l] >= arr[i]) //here
largest = l;
if (r <= n && arr[r] <= arr[i]) //here
largest = r;
if (largest != i)
{
int temp = arr[i]; //here
arr[i] = arr[largest]; //here
arr[largest] = temp; //here
}
heapify(arr, n, largest); //here
}
void buildh(int *arr, int n, int r, int c)
{
int i;
for (i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i); //here
output(arr, r, c); //here
}
void output(int *arr, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d", arr[i * c + j]); //here
}
printf("\n");
}
}
int main()
{
//...
buildh(arr, n, r, c); //here
}
i defined a matrix into a function. how do i return that matrix for print it when i call it with another function. i mean...
#include<stdio.h>
#include<conio.h>
#include<time.h>
void main() {
int m,n;
printf("type 2 numbers:");
scanf("%i %i",&m,&n);
declaration(m,n);\\HERE IS THE PROBLEM
printing(matrix,m,n);
getch();
}
void declaration(int a,int b) {
srand(time(NULL));
int i,j,matrix[a][b];
for(i=0;i<a;i++){
for(j=0;j<b;j++){
matrix[i][j]=1+rand()%7;
}
}
}
void printing(int c[100][100],int a,int b) {
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%i\t",c[i][j]);
}
printf("\n");
}
}
Define it like:
typedef struct {
int rows;
int cols;
int *data;
} int_matrix_entity, *int_matrix;
int_matrix int_matrix_create(int rows, int cols, bool rand)
{
int_matrix mt;
int i;
if ((mt = malloc(sizeof(int_matrix_entity))) == NULL)
{
return NULL;
}
if ((mt->data = malloc(sizeof(int) * cols * rows)) == NULL)
{
free(mt);
return NULL;
}
if (rand)
{
srand(time(NULL));
for (i = 0; i < cols * rows; i++)
{
mt->data[i] = 1 + rand() % 7;
}
}
else
{
memset(mt->data, 0, sizeof(int) * cols * rows);
}
return mt;
}
void int_matrix_printf(int_matrix mt)
{
int i;
int j;
for (i = 0; i < mt->rows; i++)
{
for (j = 0; j < mt->cols; j++)
{
printf("%5d ", mt[i * cols + j]);
}
printf("\n");
}
}
You have a few points that require a bit more attention;
1 ) read warning and error messages given by your compiler
2 ) again, read warning messages given by your compiler
3 ) use indentation to make your code more readable.
4 ) Always return from main(), that's a good practice
The code below does what you want to achieve; have a look at it and keep on reading...
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// You either have to declare your functions
// or implement them before main()
void declaration(int a,int b, int m[a][b]);
void printing(int a,int b, int m[a][b]);
int main(){ // always return from main()
int m,n;
printf("type 2 numbers:");
scanf("%i %i",&m,&n);
int matrix[m][n];
declaration(m, n, matrix);
printing(m, n, matrix);
return 0;
}
void declaration(int a,int b, int m[a][b]){
srand(time(NULL));
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
m[i][j]=1+rand()%7;
}
}
}
void printing(int a,int b, int m[a][b]){
int i,j;
for(i=0;i<a;i++){
for(j=0;j<b;j++){
printf("%i\t",m[i][j]);
}
printf("\n");
}
}
You need a way to transfer data from one function to another. You cannot simply declare an auto variable in one function and pass it to another as you did in the code below
declaration(m,n);
printing(matrix,m,n); /* where does matrix[][] come from? */
remember, C is a strongly typed language which means you have to declare your variables before using them. This applies to your functions as well. You either have to give your function declarations before main() (or more specifically, before using them), or implement them.
Look into your header files (i.e. .h files) and you will see lots of function declarations.
Since you use variable length arrays, make sure your compiler is at least capable of compiling code confirming C99 standard.
Some extras;
Normally, C passes arguments by value and you have to use a pointer if you want the value of your variable get changed within the function. If you have a close look at the code snippet I gave, I simply used an int m[a][b].In C, the name of an array is a pointer to its first element, hence you can change the value of array elements when actually array's name is passed to your function as an argument.
For further reading, you may want to look at
variable scope
global variables (you can define matrix[][] as a global variable and change the value of matrix elements)
declaration vs definition in C
Another simple way to do it is use double pointer to create 2-dimensional array. Keep it simple.
#include <stdio.h>
#include <stdlib.h>
int** create_matrix(int rows, int cols) {
int **matrix = malloc(rows*(sizeof(int *)));
for(int i = 0; i < rows; i++) {
matrix[i] = malloc(cols*sizeof(int));
}
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrix[i][j] = 1 + rand()%7;
}
}
return matrix;
}
void printing(int** matrix, int rows, int cols) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main(void) {
int rows, cols;
rows = 3, cols = 3;
int** matrix = create_matrix(rows, cols);
printing(matrix, rows, cols);
free(matrix);
return 0;
}
I coded as below to print all the permutations of three number :1,2,3.
But the output is:
1,1,1
1,1,2
1,1,3
1,2,1
1,2,2
1,2,3
The code is as follows:
#include<stdio.h>
#include<conio.h>
void perm(int);
int a[10],l=2;
int main()
{
int k;
k=0;
perm(k);
getch();
return 0;
}
void perm(int k)
{
int i;
for(a[k]=1;a[k]<=3;a[k]++)
{
if(k==2)
{
for(i=0;i<3;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
else
{
k++;
perm(k);
}
}
}
Please give the correct code.
Why do you increment k? k should not change for a given call to perm().
Also it's a bit too bad to be stuck with 3 permutations, you can easily generalize this way:
#include<stdio.h>
#include<conio.h>
static void perm(int, int);
static void all_perm(int);
int a[10];
int main()
{
all_perm(3);
getch();
return 0;
}
void all_perm(int n)
{
perm(0, n);
}
void perm(int k, int n)
{
if (k == n)
{
for(int i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
else
{
for(a[k]=1; a[k] <= n; a[k]++)
{
perm(k + 1, n);
}
}
}
Edit: Well, what you name permutations are not permutations.
The logic that I have used is to some extent similar to yours.
I have included the entire code to make it clear.
#include <stdio.h>
void recn(int*,int,int);
void print_arr(int*,int);
void main()
{
int arr[3] = {1,2,3};
recn(arr,3,0);
}
void print_arr(int *arr, int n){
int i;
for(i = 0,printf("\n"); i < n; printf("%d",arr[i++]));
}
void recn(int *arr, int n, int l) {
int i, j, f, k, xx = 0;
static int tst[15], a[14]={0};
if (l == n) {
for (i = 0; i < n; i++) {
tst[i] = arr[a[i]];
}
print_arr(tst,n);
return;
}
for (i = 0; i < n; i++) {
f = 0;
for (j = 0; j < l; j++)
if (a[j] == i)
f = 1;
if (!f) {
a[l] = i;
recn(arr, n, l + 1);
}
}
}
The point of the program is to send data from 1 array to another array I'm not sure what's wrong with how I'm passing it. It should enter the data in 1 array then call upon the copy function and puts itself there and then the array is traversed.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<limits.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>
double copy_arr(double source[n][u],double target[n][u],int n,int u);
int main(void)
{
double source[3][5]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][5];
copy_arr(source,target1,3,5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
double copy_arr(double source[][],double target[][],int n,int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
return target[n][u];
}
Your function prototype is wrong because compiler has not seen n and u yet. Your program does not even compile.
Change
double copy_arr(double source[n][u],double target[n][u],int n,int u);
to
double copy_arr(int n,int u,double source[n][u],double target[n][u]);
Or you could do hardcoded array size
#define SIZE_ARR 5
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u);
int main(void)
{
double source[3][SIZE_ARR]={{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5},
{1.1,2.2,3.3,4.4,5.5}};
double target1[3][SIZE_ARR];
copy_arr(source,target1, 3, 5);
int j;
int i;
for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
{
printf("%f 1",target1[i][j]);
}
}
return 0;
}
void copy_arr(double source[][SIZE_ARR], double target[][SIZE_ARR], int n, int u)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<u;j++)
{
target[i][j] = source[i][j];
}
}
}