The section of function is never entered in the following code
please help me out!
#include<stdio.h>
int findMax(int *a[], int m, int n)//this function is not entering
{
int i,j,k=0;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(k<a[i][j])
k=a[i][j];
printf("hi");//this hi is also not printing
//this hi is just for testing
return k;
}
//This function correct it if possible
int main()
{
int m,n,a[50][50],i,j,k=0;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
printf("The matrix is");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
}
k=findMax((int **)a,m,n);//statements after this is never running but no
//compilation errors
printf("\nThe maximum element in the matrix is %d",k);
return 0;
}
please help me out!!
Thanks to you in advance!!
what you are doing here: int findMax(int *a[], int m, int n) is trying to point at int 50 times, but you want to point at a variabile type int 50 times. therefor you want to decleare the pointer before you decleare the array.
use therefor:
int findMax(int (*a)[], int m, int n)
this should work now without the casting
just turn it into:
k=findmax(a,m,n);
here is a simple tutorial on the subject.
int findMax(int *a[], int m, int n)
int *a[] is an array of pointers to int, you want a pointer to an array of 50 int's:
int findMax(int (*a)[50], int m, int n)
or
int findMax(int a[][50], int m, int n)
You don't need the cast, call it using:
k = findMax(a, m, n);
Or you can use a flat array, an example:
#include <stdio.h>
void foo(int *arr, int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf("%d ", arr[i * cols + j]);
printf("\n");
}
}
#define ROWS 3
#define COLS 3
int main(void)
{
int arr[ROWS * COLS];
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++)
arr[i * COLS + j] = i * COLS + j;
}
foo(arr, ROWS, COLS);
return 0;
}
If you dont know the sizes of the 2d array beforehand you can use malloc:
#include <stdio.h>
#include <stdlib.h>
void foo(int *arr, int rows, int cols)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf("%d ", arr[i * cols + j]);
printf("\n");
}
}
int main(void)
{
int i, j, rows, cols;
int *arr;
printf("Num of rows:");
scanf("%d", &rows);
printf("Num of cols:");
scanf("%d", &cols);
arr = malloc(rows * cols);
if (arr == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
arr[i * cols + j] = i * cols + j;
}
foo(arr, rows, cols);
free(arr);
return 0;
}
#include <stdio.h>
#include <limits.h>
#define MATRIX_SIZE 50
int findMax(int a[MATRIX_SIZE][MATRIX_SIZE], int m, int n){
//Valid any of the following is
//int a[MATRIX_SIZE][MATRIX_SIZE]
//int a[][MATRIX_SIZE]
//int (*a)[MATRIX_SIZE]
int i, j, k=INT_MIN;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(k<a[i][j])
k=a[i][j];
return k;
}
int main(void){
int m, n, a[MATRIX_SIZE][MATRIX_SIZE], i, j, k=0;
printf("Enter the number of rows(<=50) in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns(<=50) in the matrix\n");
scanf("%d",&n);
if(m < 1 || n < 1 || m > 50 || n > 50){
printf("invalid input!\n");
return -1;
}
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("The matrix is");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
}
k=findMax(a, m, n);
printf("\nThe maximum element in the matrix is %d\n", k);
return 0;
}
Related
I'm very new to C and I can't quite figure out this problem.
I have to use the following function to copy every third element in input array a1[] of length n into output array a2[]
void decimate_by3(int a1[], int n, int a2[])
In the main function:
Ask user to input length of array, and elements in the array.
Calculate length of output array and declare output array.
Call decimate_by3 function
Display output array
I also have constraints which are:
I have to use the defined `void decimate_by3(int a1[], int n, int a2[]) function as is (no modifications)
I am not allowed to declare any other functions
I'm not sure where i'm going wrong but i've attached my work so far.. any help is appreciated :)
#include <stdio.h>
void decimate_by3 (int a1[], int n, int a2[])
int main (void)
{
int n, i;
printf("Enter the length of the array: ");
scanf("%d", &n);
int a1[n];
printf("Enter %d numbers: ", n);
{for (i = 0; i < n; i++)
scanf("%d", &a1[i]);}
int a2[];
if (n % 3 == 0)
int a2[n/3];
decimate_by3 (a1, n, a2);
printf("Output: ");
for (i = 0; i <= (n/3); i++)
{printf(" %d", a2[i]);}
else
int a2[(n/3)+1];
decimate_by3 (a1, n, a2);
printf("Output: ");
for (i = 0; i < n; i++)
{printf(" %d", a2[i]);}
printf("\n");
return 0;
}
void decimate_by3 (int a1[], int n, int a2[])
{
int i;
for (i = 0; i < n; i++)
{
if (i % 3 == 0)
a1[i] = a2[i];
else
delete;
}
}
You will get an error in this line because of a missing semi-colon:
void decimate_by3 (int a1[], int n, int a2[])
You also cannot declare an empty array in C. So this will raise an error:
int a2[];
This code should achieve what you want to do:
#include <stdio.h>
void decimate_by3 (int a1[], int n, int a2[]);
int main (void){
int n, i;
printf("Enter the length of the array: ");
scanf("%d", &n);
int a1[n];
printf("Enter %d numbers: \n", n);
for (i = 0; i < n; i++){
printf("Enter a number: ");
scanf("%d", &a1[i]);
}
int a2[n/3];
decimate_by3(a1, n, a2);
for(i = 0; i < n/3; i++){
printf("%d ", a2[i]);
}
printf("\n");
return 0;
}
void decimate_by3(int a1[], int n, int a2[]){
int i, j = 0;
for(i = 0; i < n; i++){
if((i+1)%3 == 0){
a2[j++] = a1[i];
}
}
}
You're almost there, couple tweaks here and there voila..
void decimate_by3(int a1[], int n, int a2[]);
int main(void) {
int n;
printf("Enter the length of the array: ");
scanf("%d", &n);
if(n <= 0)
return 0;
int a1[n];
printf("Enter %d numbers: ", n);
for (int i = 0; i < n; i++)
scanf("%d", &a1[i]);
int a2[n / 3 + 1];
decimate_by3(a1, n, a2);
printf("Output: ");
for (int j = 0; j < n/3+1; j++)
printf("%i ", a2[j]);
return 0;
}
void decimate_by3(int a1[], int n, int a2[]) {
int cnt = 0;
for (int i = 0; i < n; i++)
if (i % 3 == 0)
a2[cnt++] = a1[i];
}
i have tried doing it with functions and arrays but have failed in-order to do it with pointers.
I want to do it with the help of pointers instead of arrays, but i am having problem with passing and calling the values of arrays using pointers.
below is the code.
#include <stdio.h>
void final_array(int arr[], int size);
void array(int arr[], int i, int size);
int main()
{
int num, size[100];
int i, j;
int arr[100][100];
printf("Enter the number of arrays: \t");
scanf("%d", &num);
num = num < 100 ? num: 100;
//feeding elements.
for (i = 0; i<num; i++)
{
printf("\nEnter the size of the array: \t");
scanf("%d", &size[i]);
printf("\nEnter the array: ");
size[i] = size[i] < 100 ? size[i] : 100;
array(&arr[i][0], i, size[i]);
}
for(i=0; i<num; i++)
{
final_array(&arr[i][0], size[i]);
printf("\n");
}
printf("\nPress Enter key to exit.\n");
getchar();
return 0;
}
void array(int arr[], int i, int size)
{
int j;
for (j = 0; j<size; j++)
{
printf("\nEnter arr[%d][%d]: \t",i, j);
scanf("%d", &arr[j]);
}
}
void final_array(int arr[], int size)
{
int j;
for(j=0; j<size; j++)
{
printf("%d\t", arr[j]);
}
}
I guess this should solve your problem.Let me know if it's correct.
#include <stdio.h>
#include <stdlib.h>
void final_array(int *arr, int size);
void array(int *arr, int i, int size);
int main()
{
int num;
int i, j;
int **arr=(int **)malloc(100 * sizeof(int *));
for (i=0; i<100; i++)
arr[i] = (int *)malloc(100 * sizeof(int));
printf("Enter the number of arrays: \t");
scanf("%d", &num);
int *size=malloc(sizeof(int)*num);
num = num < 100 ? num: 100;
//feeding elements.
for (i = 0; i<num; i++)
{
printf("\nEnter the size of the array: \t");
scanf("%d", &size[i]);
printf("\nEnter the array: ");
size[i] = size[i] < 100 ? size[i] : 100;
array(&arr[i][0], i, size[i]);
}
for(i=0; i<num; i++)
{
final_array(&arr[i][0], size[i]);
printf("\n");
}
printf("\nPress Enter key to exit.\n");
getchar();
return 0;
}
void array(int *arr, int i, int size)
{
int j;
for (j = 0; j<size; j++)
{
printf("\nEnter arr[%d][%d]: \t",i, j);
scanf("%d", &arr[j]);
}
}
void final_array(int *arr, int size)
{
int j;
for(j=0; j<size; j++)
{
printf("%d\t", arr[j]);
}
}
I did this program for a homework and it crashes without any error when run.
Also after correcting it, any suggestions to increase the efficiency of my coding approach are appreciated.
First I declared m,n,p,q as global variables an I passed only the arrays to the functions, but the program behaved weird.
Then I included the dimensions of the arrays as arguments in every function and declared it everywhere. CRASH
*VLA SUPPORTED
//functions on matrices
#include<stdio.h>
int i, j;
void getMatrix(int m, int n, int values[m][n]);
void displayMatrix(int m, int n, int values[m][n]);
void transposeMatrix(int m, int n, int values[m][n]);
void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]);
void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q]);
int main()
{
int m, n, p, q, A[m][n], B[p][q];
printf("Enter the no. of Rows of the first Matrix : ");
scanf("%d", &m);
printf("Enter the no. of Columns of the first Matrix : ");
scanf("%d", &n);
printf("Enter the elements of the first matrix: \n");
getMatrix(m, n, A);
printf("The entered Matrix:\n");
displayMatrix(m, n, A);
printf("The transpose of the entered Matrix:\n");
transposeMatrix(m, n, A);
printf("Enter the no. of Rows of the second Matrix : ");
scanf("%d", &p);
printf("Enter the no. of Columns of the second Matrix : ");
scanf("%d", &q);
printf("Enter the elements of the secong matrix: \n");
getMatrix(p, q, B);
printf("The entered Matrix:\n");
displayMatrix(p, q, B);
printf("The transpose of the entered Matrix:\n");
transposeMatrix(p, q, B);
printf("Addition of the Matrices:\n");
addMatrices(m, n, p, q, A, B);
printf("Multiplication of the Matrices:\n");
multiplyMatrices(m, n, p, q, A, B);
return 0;
}
void getMatrix(int m, int n, int values[m][n])
{
for(i = 0; i < m; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &values[i][j]);
}
void displayMatrix(int m, int n, int values[m][n])
{
for(i = 0; i < m; ++i)
{
for(j = 0; j < n; ++j)
printf("%3d ", values[i][j]);
printf("\n");
}
}
void transposeMatrix(int m, int n, int values[m][n])
{
int transpose[n][m];
for(i = 0; i < n; ++i)
for(j =0; j < m; ++j)
transpose[i][j] = values[j][i];
displayMatrix(n, m, transpose);
}
void addMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q])
{
int C[m][n];
if(m == p && n == q)
{
for(i = 0; i < m; ++i)
for(j = 0; j < n; ++j)
C[i][j] = A[i][j] + B[i][j];
displayMatrix(m, n, C);
}
else
printf("Cannot add these Matrices!\n");
}
void multiplyMatrices(int m, int n, int p, int q, int A[m][n], int B[p][q])
{
int C[m][q], k, sum = 0;
if(n == p)
{
for(i = 0; i < m; ++i)
for(j = 0; j < q; ++j)
{
for(k = 0; k < n; ++k)
sum += A[i][j] * B[j][i];
C[i][j] = sum;
sum = 0;
}
displayMatrix(m, q, C);
}
else
printf("Cannot multiply these Matrices!\n");
}
Initiliazie m and n so that you don't get UB when using them in array index in VLA.
Unsed loop in multiply matrices
for(k = 0; k < n; ++k)
sum += A[i][j] * B[j][i];
will be
for(k = 0; k < n; ++k)
sum += A[i][k] * B[k][j];
Don't use global variables unless you need to. It is good practice to make index variables of for loop local.
for(int i=0;i<n;i++)
...
This program is supposed to take 2 arrays and perform the dot product on each of the elements in the array.
My program is fine if the index of n is less than 5; however, once the index of the array is greater than 5 only the first element in the first array is wrong ( I checked by adding a printf statement in the function). I don't know how to fix this bug.
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
int v1[n];
int v2[n];
int v3[n];
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
printf("Enter numbers for the first array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
Correct code
#include <stdio.h>
void multi_vec(int *v1, int *v2, int *v3, int n);
int main(void)
{
int n, i;
printf("Enter the length of the two vectors\n");
scanf("%d", &n);
int v1[n],v2[n],v3[n]; //you didn't initialize n
printf("Enter numbers for the first array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
}
printf("Enter numbers for the second array\n"); //printf statements had extra ',n'
for (i = 0; i < n; i++) {
scanf("%d", &v2[i]);
}
multi_vec(v1, v2, v3, n);
for (i = 0; i < n; i++) {
printf("%d ", v3[i]);
}
printf("\n");
return 0;
}
void multi_vec(int *v1, int *v2, int *v3, int n)
{
int i;
for (i = 0; i < n; i++) {
*(v3+i) = *(v1+i) * *(v2+i);
}
}
#include<stdio.h>
int findMax(int **a,int r,int c);
int main()
{
int a[10][10],i,j,max,r,c;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&r);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&c);
printf("Enter the elements in the matrix\n");
for(i=1;i<=r;i++)
{ for(j=1;j<=c;j++)
scanf("%d",&[i][j]);
}
printf("The matrix is\n");
for(i=1;i<=r;i++)
{ for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
}printf("\n");}
max=findMax((int **)a,r,c);
printf("The maximum elements in the matrix is %d\n",max);
return 0;
}
int findMax(int **a,int r,int c)
{
int t,i,j;
t=a[1][1];
for(i=1;i<r;i++)
{ for(j=1;j<c;j++)
{ if(a[i][j]>t)
t=a[i][j];
}
}
return (t);
}
Here I attached my coding, I need to find the maximum element present in the matrix using function, I am doing the coding, calling function is not executed, I don't know why, Help me to figure it out.
Change
int findMax(int **a,int r,int c)
to
int findMax(int (*a)[10],int r,int c)
And also,
for(i=1;i<r;i++)
{
for(j=1;j<c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
to
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
EDIT:
I think, your code should be like this:
#include<stdio.h>
int findMax(int (*a)[10],int r,int c);
int main()
{
int a[10][10],i,j,mx,r,c;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&r);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&c);
printf("Enter the elements in the matrix\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
scanf("%d",&a[i][j]);
}
printf("The matrix is\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
printf("%d ",a[i][j]);
printf("\n");
}
printf("\n");
mx=findMax(a,r,c);
printf("The maximum elements in the matrix is %d\n",mx);
return 0;
}
int findMax(int (*a)[10],int r,int c)
{
int t,i,j;
t=a[1][1];
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(a[i][j]>t)
t=a[i][j];
}
}
return (t);
}
Hope, it will help. :)
You can not pass a 2d array (int a[10][10]) to a pointer to pointer.
int findMax(int **a, int r, int c)
should be
int findMax(int (*a)[10], int r, int c) /* Pointer to array of 10 ints */
Use the heap if you don't know the size of the 2d array beforehand (and note that arrays are base 0):
#include <stdio.h>
#include <stdlib.h>
int findMax(int *a, int r, int c);
int main(void)
{
int *a, r, c, i, j, max;
printf("Enter the number of rows in the matrix\n");
scanf("%d", &r);
printf("Enter the number of columns in the matrix\n");
scanf("%d", &c);
a = malloc(r * c * sizeof(*a));
if (a == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
printf("Enter the elements in the matrix\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
scanf("%d", &a[i * c + j]);
}
printf("The matrix is\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++)
printf("%d", a[i * c + j]);
}
printf("\n");
max = findMax(a, r, c);
printf("The maximum elements in the matrix is %d\n", max);
free(a);
return 0;
}
int findMax(int *a,int r, int c)
{
int t, i, j;
t = a[0];
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
if(a[i * c + j] > t)
t = a[i * c + j];
}
}
return t;
}
Or you can use a variable-length-array if you are under C99:
#include <stdio.h>
int findMax(int r, int c, int (*a)[]);
int main(void)
{
int i, j, max, r, c;
printf("Enter the number of rows in the matrix\n");
scanf("%d", &r);
printf("Enter the number of columns in the matrix\n");
scanf("%d", &c);
int a[r][c];
printf("Enter the elements in the matrix\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
}
printf("The matrix is\n");
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++)
printf("%d", a[i][j]);
}
printf("\n");
max = findMax(r, c, a);
printf("The maximum elements in the matrix is %d\n", max);
return 0;
}
int findMax(int r, int c, int (*a)[c])
{
int t, i, j;
t = a[0][0];
for(i = 0; i < r; i++) {
for(j = 0; j < c; j++) {
if(a[i][j] > t)
t = a[i][j];
}
}
return t;
}
This code snippet works for you.
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
a[i]=malloc(sizeof(int)*c);
for(j=0;j<n;j++)
scanf("%d", &a[i][j]);
}
Then your function call max=findMax(a,r,c); will work.
#include<stdio.h>
#include<stdlib.h>
int findMax(int **a,int m,int n)
{
int i,j,larg;
larg=a[0][0];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(larg<a[i][j])
larg=a[i][j];
}
}
return larg;
}
int main()
{
int m,n,i,j,larg;
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
int **a=(int**)malloc(m*sizeof(int *));
for(i=0;i<m;i++)
a[i]=(int *)malloc(n*sizeof(int));
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d\n",&a[i][j]);
larg=findMax(a,m,n);
printf("The matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",larg);
return 0;
}