Trouble with array in function - c

I'm having trouble with a program that takes a 2d array, calculates the average of each row, then stores these averages in a new array. Where I seem to be having the problem is with actually assigning the averages to the new 1d array. If I put a print statement in the loop directly after the value has been assigned it seems to be there and work fine. But once I try to display whats in the array with my created display function it is all zeroes except for one entry that's a random large number.
Sample Output:
36.25 58.00 52.00 42.00 51.75 47.00 <--- From print within average function (Correct)
0.000 0.000 0.000 0.000 406778500087808.000 0.000 <--- From created display function
void row_average(int vals2d[][4], int rows, int cols)
{
int k, l, sum;
float avg, rowavg[6];
for (k=0; k<rows; k++){
sum = 0;
avg = 0;
for (l=0; l<cols; l++){
sum = sum + vals2d[k][l];
avg = (float)sum/4;
}
rowavg[k] = avg;
printf("%.2f ", rowavg[k]);
}
}
void array_1display(float rowavg[], int size)
{
int m;
for (m=0; m<size; m++){
printf("%.3f ", rowavg[m]);
}
}
int main()
{
float rowavg[6];
int vals2d[6][4] ={
{12,54,34,45},
{68,76,65,23},
{75,23,76,34},
{6,45,58,59},
{35,67,93,12},
{66,90,25,7}
};
row_average(vals2d, 6, 4);
printf("\n");
array_1display(rowavg, 6);
}

You need to pass your array rowavg[] or make it global.
Here is some correction:
#include <stdio.h>
void row_average(int vals2d[][4], int rows, int cols, float rowavg[])
{
int k, l, sum;
float avg;
for (k=0; k<rows; k++){
sum = 0;
avg = 0;
for (l=0; l<cols; l++){
sum = sum + vals2d[k][l];
avg = (float)sum/4;
}
rowavg[k] = avg;
printf("%.2f ", rowavg[k]);
}
}
void array_1display(float rowavg[], int size)
{
int m;
for (m=0; m<size; m++){
printf("%.3f ", rowavg[m]);
}
}
int main()
{
float rowavg[6];
int vals2d[6][4] ={
{12,54,34,45},
{68,76,65,23},
{75,23,76,34},
{6,45,58,59},
{35,67,93,12},
{66,90,25,7}
};
row_average(vals2d, 6, 4, rowavg);
printf("\n");
array_1display(rowavg, 6);
}

Related

Error when passing 2D array to function in c

I am writing a program to calculate matrix multiplication but it does not work. When I debug and check each value of the array a and b in function printMatrixMultiplication (which are entered by user), GDB prints out "cannot perform pointer math on incomplete type try casting". (I have searched for it but I still don't get it.) The function only works when the input is predefined in main.
This is my code
#include <stdio.h>
void input(int m, int n, double a[m][n]);
void output(int m, int n, double a[m][n]);
void printMatrixMultiplication(int row_a, int col_a, double a[row_a][col_a], int row_b, int col_b, double b[row_b][col_b]);
int main()
{
int row_a, col_a, row_b, col_b;
// get value of matrix a
printf("row_a = ");
scanf("%d", &row_a);
printf("col_a = ");
scanf("%d", &col_a);
double a[row_a][col_a];
input(row_a, col_a, a);
// output(row_a, col_a, a);
// get value of matrix b
printf("row_b = ");
scanf("%d", &row_b);
printf("col_b = ");
scanf("%d", &col_b);
double b[row_b][col_b];
input(row_b, col_b, a);
// output(row_b, col_b, a);
printMatrixMultiplication(row_a, col_a, a, row_b, col_b, b);
//test
// double a[2][2]={1,2,3,4};
// double b[2][3]={1,2,3,4,5,6};
// printMatrixMultiplication(2,2,a,2,3,b);
return 0;
}
void input(int m, int n, double a[m][n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%lf", &a[i][j]);
}
}
}
void output(int m, int n, double a[m][n])
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%.2f ", a[i][j]);
}
printf("\n");
}
}
void printMatrixMultiplication(int row_a, int col_a, double a[row_a][col_a], int row_b, int col_b, double b[row_b][col_b])
{
if (col_a != row_b)
{
return;
}
double res[row_a][col_b]; //this matrix store results
for (int i = 0; i < row_a; i++) //the values be stored line by line, this
{ //operation is controled by i and j loops.
for (int j = 0; j < col_b; j++) //the k loop helps calculate dot_product.
{
double dot_product = 0;
for (int k = 0; k < col_a; k++)
{
dot_product += a[i][k] * b[k][j]; //ERROR HERE
}
res[i][j] = dot_product;
}
}
output(row_a, col_b, res);
}
So, where does the error come from and how to fix it?
Irrelevant, but the function is not well implemented so if possible, I would really appreciate if anyone gives me a hint to improve it.
I am using GCC version 6.3.0.
It's typo in your code when reading matrix b.
Just replace:
input(row_b, col_b, a);
with
input(row_b, col_b, b);

Function will not call correctly, C

I am a beginner and am doing pretty bad in my class right now, I just can't get some of this stuff down. I am working on one of the final homeworks and can't figure out the problem with my code. It's probably really messy but the main thing I'm having trouble with right now is the getArea function. It won't run correctly when I call it and says 'expected expression before int'. Any help is greatly appreciated, thanks
//This program takes 18 numbers and puts it in a single dimensional array and a two dimensional array.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define COLMAX 3
#define ROWMAX 6
double triArea(double a, double b, double c);
double checkValidation(double a, double b, double c);
double func1(double values[][COLMAX], int);
int main(void)
{
unsigned int seed;
double tArea = 0.0;
int rand_int();
int loop;
int col;
int row;
int counter = 0;
srand(time(NULL));
int RandomArray[18];
int ArrayTwo[6][3];
printf("\nOne dimensional array\n");
for(loop = 0; loop < 18; loop++)
{
RandomArray[loop] = rand_int();
printf("%d ", RandomArray[loop]);
}
printf("\nTwo dimensional array\n");
for(row = 0; row < ROWMAX; row++)
{
for(col = 0; col < COLMAX; col++)
{
ArrayTwo[row][col] = RandomArray[counter];
counter++;
//int total = 0;
//total = total + array2[row][col];
printf("%d\t", ArrayTwo[row][col]);
getArea(ArrayTwo[row][col]);
//printf("The total is %d", total);
}
printf("\n");
}
printf("\n");
double total = 0.0;
double ArrayTwoTotal[ROWMAX][COLMAX];
total = func1(ArrayTwoTotal, ROWMAX);
printf("total is %.2lf \n", total);
system ("PAUSE");
return 0;
}
double checkValidation(double a, double b, double c)
//triangleValueA + triangleValueB >= triangleValueC) && (triangleValueB + triangleValueC >= triangleValueA) && (triangleValueA + triangleValueC >= triangleValueB
{
int count = 0;
//a = base, b = height, c = area
if ((a + b >= c) && (b + c >= a) && (a + c >= b))
{
}
}
//Calculate area of a triangle
double getArea(int[int][int])
{
double base, height, area;
area = base*height/2.0;
printf("The area is %.lf\n", area);
checkValidation(base, height, area);
return area;
}
//Function to get the totals of the 1 dimensional array
double func1(double ArrayTwoTotal[][COLMAX], int rows)
{
double sum = 0.0;
int i, j;
for(i = 0 ; i < rows ; i++)
{
for(j = 0 ; j < COLMAX ; j++)
{
sum += ArrayTwoTotal[i][j];
}
}
return sum;
}
//Function to find a random number
int rand_int()
{
int x = 0;
x = ((rand() % 100)+1);
return x;
}
There are a lot of things that you need to look at.
First getArea() function doesn't have any declaration. Please declare it first and in a correct way.

matrix multipication in c

i know there are similiar questions but i tried the suggestions there and it still does not work, here my complete code;
#include <stdio.h>
#include <math.h>
void ludec(int N, double lu[N][N]){
double temp;
int k,i,j;
for(k=0;k<N;k++){
for(i=k+1;i<N;i++){
temp=lu[i][k]/=lu[k][k];
for(j=k+1;j<N;j++){
lu[i][j]-=temp*lu[k][j];
}
}
}
}
void matmul(int u,int m,int n,double p[u][m],double bb[m][n]){
double mul[u][n];
int ii,jj,kk;
for(ii=0;ii<u;ii++){
for(jj=0;jj<n;jj++){
mul[ii][jj]=0.0;
}
}
for(ii=0;ii<u;ii++){
for(jj=0;jj<n;jj++){
for(kk=0;kk<m;kk++){
mul[ii][jj]=mul[ii][jj]+p[ii][kk]*bb[kk][jj];
printf("la %f ii %d jj%d kk%d\n",mul[ii][jj],ii,jj,kk);
}
}
}
}
int main(void){
double lu[3][3]={{4.0,3.0,-2.0},{-1.0,-1.0,3.0},{2.0,-1.0,5.0}};
double a[3][3]={{4.0,3.0,-2.0},{-1.0,-1.0,3.0},{2.0,-1.0,5.0}};
double y[3][1];
double b[3][1]={{9.0},{-4.0},{6.0}};
double x[3][1];
int i,j,N=3;
double f=0;
ludec(N, lu);
y[0][0]=b[0][0];
for(i=1;i<3;i++){
for(j=0;j<i;j++){
f+=lu[i][j]*y[j][0];
}
y[i][0]=(b[i][0]-f);
f=0;
}
f=0;
x[2][0]=y[2][0]/lu[2][2];
for(i=1;i>=0;i--){
for(j=1+i;j<3;j++){
f+=lu[i][j]*x[j][0];
}
x[i][0]=(y[i][0]-f)/lu[i][i];
f=0;
}
printf("%f\n %f\n %f\n",x[0][0],x[1][0],x[2][0]);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%f %d %d\n",lu[i][j],i,j);
}
}
double mul[3][1];
matmul(3,3,1,a,x);
for(i=0;i<3;i++){
for(j=0;j<2;j++){
printf("%f %d %d\n",mul[i][j],i,j);
}
}
return 0.0;
}
here is the part responsible by multiplication;
the problem is
i have to get 3*1 matrix as result in matrix multiplication, but it gives bigger matrix with alot of garbage elements i monitor the matrix multiplication by printf in the matmul function and it seems to work but when i do same out side the matmul function it gives garbage only some values are correct with a lot of useless wrong elements.
void matmul(int u,int m,int n,double p[u][m],double bb[m][n]){
double mul[u][n];
int ii,jj,kk;
for(ii=0;ii<u;ii++){
for(jj=0;jj<n;jj++){
mul[ii][jj]=0.0;
}
}
for(ii=0;ii<u;ii++){
for(jj=0;jj<n;jj++){
for(kk=0;kk<m;kk++){
mul[ii][jj]=mul[ii][jj]+p[ii][kk]*bb[kk][jj];
printf("la %f ii %d jj%d kk%d\n",mul[ii][jj],ii,jj,kk);
}
}
}
}
the problem is matmul function and i dont know why it does not work i already initialized the matrix with zeros. am i using void function property wrong?
i am very new to c hence there may be something i am doing wrong. the overall code is for lu decompositation, and i want to see wheter the code works or not by multiplying result with initial matrix.
this is the out put
mul is a local variable in matmul, thus the result is not visible to the calling function. You should modify the matmul function to take the result matrix as an argument:
void matmul(int u, int m, int n,
double mul[u][n], const double p[u][m], const double bb[m][n])
{
for (int i = 0; i < u; i++) {
for (int j = 0; j < n; j++) {
mul[i][j] = 0.0;
for (int k = 0; k < m; k++) {
mul[i][j] += p[i][k] * bb[k][j];
}
}
}
}
Invoke from main as
double mul[3][1];
matmul(3, 3, 1, mul, a, x);
And print the output vector:
for (i = 0; i < 3; i++) {
for (j = 0; j < 1; j++) {
printf("%f %d %d\n", mul[i][j], i, j);
}
}

Matrix Multiplication for any proper order with dynamic memory allocation

I am trying to perform matrix multiplication(dynamic memory allocation) where that the user can enter any valid order for matrix multiplication (i.e. column1=row2).
The output for same orders (2x2 or 3x3) for both matrices result in proper computation, but the orders like mat1 2x3 & mat2 3x2..give segmentation fault. I am not able to ascertain how am I accessing any illegal memory when I am doing the memory allocation beforehand.
kindly advise, pls forgive me if I am making some silly mistake...
following is the complete code:
#include<stdio.h>
#include<stdlib.h>
main(){
int **mat1, **mat2,**res,i,j,r1,c1,r2,c2;
printf("\nEnter the Order of the First matrix...\n");
scanf("%d %d",&r1,&c1);
printf("\nEnter the Order of the Second matrix...\n");
scanf("%d %d",&r2,&c2);
if(c1!=r2){
printf("Invalid Order of matrix");
exit(EXIT_SUCCESS);
}
mat1= (int**) malloc(r1*sizeof(int*));
for(i=0;i<c1;i++)
mat1[i]=(int*)malloc(c1*sizeof(int));
mat2= (int**) malloc(r2*sizeof(int*));
for(i=0;i<c2;i++)
mat2[i]=(int*)malloc(c2*sizeof(int));
res=(int**)calloc(r1,sizeof(int*));
for(i=0;i<c2;i++)
res[i]=(int*)calloc(c2,sizeof(int));
//Input Matrix1
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&mat1[i][j]);
//Input Matrix2
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&mat2[i][j]);
//Printing Input Matrix 1 and 2
printf("\n Entered Matrix 1: \n");
for(i=0;i<r1;i++){
for(j=0;j<c1;j++)
printf("%d ",mat1[i][j]);
printf("\n");
}
printf("\n Entered Matrix 2: \n");
for(i=0;i<r2;i++){
for(j=0;j<c2;j++)
printf("%d ",mat2[i][j]);
printf("\n");
}
//Computation
//Multiplication
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
res[i][j]=0;
for(k=0;k<c1;k++)
res[i][j]+= mat1[i][k]*mat2[k][j];
}
printf("\n");
}
printf("\nThe Multiplication of two matrix is\n");
for(i=0;i<r1;i++){
printf("\n");
for(j=0;j<c2;j++)
printf("%d\t",res[i][j]);
}
printf("\n");
/* Addition
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
res[i][j]=mat1[i][j]+mat2[i][j];
printf("\nThe Addition of two matrix is\n");
for(i=0;i<r1;i++){
printf("\n");
for(j=0;j<c2;j++)
printf("%d\t",res[i][j]);
}
*/
return 0;}
mat1= (int**) malloc(r1*sizeof(int*));
for(i=0;i<c1;i++) < c1 instead of r1
mat1[i]=(int*)malloc(c1*sizeof(int));
mat2= (int**) malloc(r2*sizeof(int*));
for(i=0;i<c2;i++) < c2 instead of r2
mat2[i]=(int*)malloc(c2*sizeof(int));
You use c1/2 in your for instead of r1/2.
If r1 < c1, you end up outside of the memory you allocated.
If r1 > c1, you end up with uninitialized pointers.
Not related to the issue but you should write int main() instead of main(), the second one is accepted but the first one easier to read.
Here is the code for any valid matrix multiplication....
feel free for "Queries"....
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int *ans,*first,*second;
int *A,*B,*C;
int i,j,k=0;
int rowA,colA,sizeA,sizeB,sizeC;
int rowB,colB;
printf("Enter the row's and column of 1st matrix\n");
scanf("%d%d",&rowA,&colA);
printf("Enter the row's and column of 2nd matrix\n");
scanf("%d%d",&rowB,&colB);
if(colA!=rowB)
{
printf("Error => colA must be equal to rowB\n");
getch();
exit(EXIT_SUCCESS);
}
sizeC = rowA*colB;
sizeA = rowA*colA;
sizeB = rowB*colB;
A = (int *)malloc(sizeA*sizeof(int *));
first = A;
B = (int *)malloc(sizeB*sizeof(int *));
second = B;
C = (int *)malloc(sizeC*sizeof(int *));
ans = C;
printf("Enter the elements of the first matrix A\n");
for(i=0;i<sizeA;i++,first++)
scanf("%d",first);
printf("Enter the elements of the second matrix B\n");
for(i=0;i<sizeB;i++,second++)
scanf("%d",second);
first=A;
second= B;
if(rowA==1 && colB==1)
{
for(i=0;i<rowA;i++)
{
for(j=0;j<colB;j++)
{
*ans=0;
for(k=0;k<rowB;k++)
*ans = *ans + (*(first + (k + i*colA))) * (*(second + (j+k*colB)));
ans++;
}//j
}//i
}//if
else
{
for(i=0;i<rowA;i++)
{
for(j=0;j<colB;j++)
{
*ans=0;
for(k=0;k<rowB;k++)
*ans = *ans + (*(first + (k + i*colA))) * (*(second + (j+k*rowB)));
ans++;
}//j
}//i
}
printf("\nThe value of matrix 'C' = \n");
ans = C;
for(i=0;i<rowA;i++)
{
printf("\n");
for(j=0;j<colB;j++,ans++)
printf("%d\t",*ans);
}
free(A);
free(B);
free(C);
getch();
}
I attached bellow the code for Matrix Multiplication for any proper order with dynamic memory allocation
For completeness I used 3 different methods for matrix multiplication: one function double** multMatrixpf (see equivalent function Fortran/Pascal) and two subroutine/procedure(Fortran/Pascal like), where by first void multMatrixp you need to allocate_mem(&c,ro1,co2) outside and in second subroutine void multMatrixpp the matrix c1 is allocated inside the subroutine. All of these three methods give the same result.
As well I used different methods for initialising arrays.
#include <stdio.h>
#include <stdlib.h>
void allocate_mem(double*** arr, int rows, int cols);
void deallocate_mem(double*** arr, int n);
double** readMatrixf(int rows, int cols);
void readMatrix(double ***a, int rows,int cols);
void printMatrix(double** a, int rows, int cols);
void printMatrixE(double** a, int rows, int cols);
void multMatrixp(double **A, double **B, double **C,int r1,int c1,int r2,int c2);
void multMatrixpp(double **A, double **B, double ***C,int ro1,int co1,int ro2,int co2);
double** multMatrixpf(double **A, double **B, int ro1,int co1,int ro2,int co2);
//______________________________________________________________________________
int main()
{
int ro1, co1, ro2, co2;
double **a1, **b1, **c1;
ro1=2; co1=3;
ro2=3; co2=4;
printf("Ex1:__________________________________________________"
"__________________________ \n");
double (*(a[])) = {
(double[]) { 1, 3, 5},
(double[]) {2, 4, 0}};
double (*(b[])) = {
(double[]) {6, 2, 4, 8},
(double[]) {1, 7, 0, 9},
(double[]) {0, 3, 5, 1}};
printMatrix(a,ro1,co1);
printMatrix(b,ro2,co2);
printf("MatMult \n");
double **c;
allocate_mem(&c,ro1,co2);
multMatrixp(a, b, c, ro1, co1, ro2, co2);
printMatrix(c,ro1,co2);
printMatrixE(c,ro1,co2);
deallocate_mem(&c,ro1);
printf("Ex2:__________________________________________________"
"__________________________ \n");
scanf("%d%d", &ro1, &co1);
readMatrix(&a1,ro1,co1);
printMatrix(a1,ro1,co1);
//deallocate_mem(&a1,ro1);
//printMatrix(a1,ro1,co1);
scanf("%d%d", &ro2, &co2);
readMatrix(&b1,ro2,co2);
printMatrix(b1,ro2,co2);
printf("MatMult \n");
multMatrixpp(a1, b1, &c1, ro1, co1, ro2, co2);
printMatrix(c1,ro1,co2);
printMatrixE(c1,ro1,co2);
deallocate_mem(&a1,ro1);
deallocate_mem(&b1,ro2);
deallocate_mem(&c1,ro1);
printf("Ex3:__________________________________________________"
"__________________________ \n");
scanf("%d%d", &ro1, &co1);
a1=readMatrixf(ro1,co1);
printMatrix(a1,ro1,co1);
//deallocate_mem(&a1,ro1);
//printMatrix(a1,ro1,co1);
scanf("%d%d", &ro2, &co2);
b1=readMatrixf(ro2,co2);
printMatrix(b1,ro2,co2);
printf("MatMult \n");
c1=multMatrixpf(a1, b1, ro1, co1, ro2, co2);
printMatrix(c1,ro1,co2);
printMatrixE(c1,ro1,co2);
deallocate_mem(&a1,ro1);
deallocate_mem(&b1,ro2);
deallocate_mem(&c1,ro1);
return 0;
}
//______________________________________________________________________________
void allocate_mem(double*** arr, int rows, int cols)
{
int i;
*arr = (double**)malloc(rows*sizeof(double*));
for( i=0; i<rows; i++)
(*arr)[i] = (double*)malloc(cols*sizeof(double));
}
//______________________________________________________________________________
void deallocate_mem(double*** arr, int rows){
int i;
for (i = 0; i < rows; i++)
free((*arr)[i]);
free(*arr);
}
//______________________________________________________________________________
double** readMatrixf(int rows, int cols)
{
double **a; // Define a local pointer to keep rest of the code intact
int i, j;
a= (double**) malloc(rows*sizeof(double*));
for(i=0;i<rows;i++)
a[i]=(double*)malloc(cols*sizeof(double));
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%lf",&a[i][j]);
return a;
}
//______________________________________________________________________________
void readMatrix(double ***a, int rows,int cols)
{
int i, j;
*a= (double**) malloc(rows*sizeof(double*));
for(i=0;i<rows;i++)
(*a)[i]=(double*)malloc(cols*sizeof(double));
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
scanf("%lf",&(*a)[i][j]);
}
//______________________________________________________________________________
void printMatrix(double** a, int rows, int cols)
{
int i, j;
printf("Matrix[%d][%d]\n",rows,cols);
for(i=0;i<rows;i++){
for(j=0;j<cols;j++)
printf("%8.3lf ",a[i][j]);
printf("\n");
}
printf("\n");
}
//______________________________________________________________________________
void printMatrixE(double** a, int rows, int cols)
{
int i, j;
printf("Matrix[%d][%d]\n",rows,cols);
for(i=0;i<rows;i++){
for(j=0;j<cols;j++)
printf("%9.2e ",a[i][j]);
printf("\n");
}
printf("\n");
}
//______________________________________________________________________________
void multMatrixp(double **A, double **B, double **C,int ro1,int co1,int ro2,int co2)
{
int i, j, k;
for(i = 0; i < ro1; i++) {
for(j = 0; j < co2; j++) {
C[i][j] = 0;
for(k = 0; k < co1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
//______________________________________________________________________________
void multMatrixpp(double **A, double **B, double ***C,int ro1,int co1,int ro2,int co2)
{
int i, j, k;
*C= (double**) malloc(ro1*sizeof(double*));
for(i=0;i<ro1;i++)
(*C)[i]=(double*)malloc(co2*sizeof(double));
for(i = 0; i < ro1; i++) {
for(j = 0; j < co2; j++) {
(*C)[i][j] = 0.0;
for(k = 0; k < co1; k++) {
(*C)[i][j] += A[i][k] * B[k][j];
}
}
}
}
//______________________________________________________________________________
double** multMatrixpf(double **A, double **B, int ro1,int co1,int ro2,int co2)
{
int i, j, k;
double **C;
C= (double**) malloc(ro1*sizeof(double*));
for(i=0;i<ro1;i++)
C[i]=(double*)malloc(co2*sizeof(double));
for(i = 0; i < ro1; i++) {
for(j = 0; j < co2; j++) {
C[i][j] = 0.0;
for(k = 0; k < co1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
where as input matrix we have in.txt
4 4
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
4 3
4.0 -3.0 4.0
-13.0 19.0 -7.0
3.0 -2.0 7.0
-1.0 1.0 -1.0
3 4
1 2 -2 0
-3 4 7 2
6 0 3 1
4 2
-1 3
0 9
1 -11
4 -5
in unix like cmmd line execute the command:
$ time ./Matmult < in.txt > out.txt
and you get the output
out.txt
Ex1:____________________________________________________________________________
Matrix[2][3]
1.000 3.000 5.000
2.000 4.000 0.000
Matrix[3][4]
6.000 2.000 4.000 8.000
1.000 7.000 0.000 9.000
0.000 3.000 5.000 1.000
MatMult
Matrix[2][4]
9.000 38.000 29.000 40.000
16.000 32.000 8.000 52.000
Matrix[2][4]
9.00e+00 3.80e+01 2.90e+01 4.00e+01
1.60e+01 3.20e+01 8.00e+00 5.20e+01
Ex2:____________________________________________________________________________
Matrix[4][4]
1.000 1.000 1.000 1.000
2.000 4.000 8.000 16.000
3.000 9.000 27.000 81.000
4.000 16.000 64.000 256.000
Matrix[4][3]
4.000 -3.000 4.000
-13.000 19.000 -7.000
3.000 -2.000 7.000
-1.000 1.000 -1.000
MatMult
Matrix[4][3]
-7.000 15.000 3.000
-36.000 70.000 20.000
-105.000 189.000 57.000
-256.000 420.000 96.000
Matrix[4][3]
-7.00e+00 1.50e+01 3.00e+00
-3.60e+01 7.00e+01 2.00e+01
-1.05e+02 1.89e+02 5.70e+01
-2.56e+02 4.20e+02 9.60e+01
Ex3:____________________________________________________________________________
Matrix[3][4]
1.000 2.000 -2.000 0.000
-3.000 4.000 7.000 2.000
6.000 0.000 3.000 1.000
Matrix[4][2]
-1.000 3.000
0.000 9.000
1.000 -11.000
4.000 -5.000
MatMult
Matrix[3][2]
-3.000 43.000
18.000 -60.000
1.000 -20.000
Matrix[3][2]
-3.00e+00 4.30e+01
1.80e+01 -6.00e+01
1.00e+00 -2.00e+01
While computing the matrix multiplication you have to run the k loop from 0 to c2, and not from 0 to c1.

Trying to find out average of marks...but getting wrong ans

Friends in following code i am trying to get some of the average in float...i tried manually my ans should be in fraction, but here it comes in rounded fig...please help me.
#include <stdio.h>
#include <stdlib.h>
//finding out the average marks for students ....
float average (float []);
int main()
{
int i;
float total_avg=0;
float arr_1[] = {10.2, 22.9, 36.04, 89.1, 94.1, 10.8,};
for(i=0; i<6; i++)
printf("%2f\n",arr_1[i]);
printf("\n\n");
total_avg = average (arr_1);
printf("Total average : %3f\n",total_avg);
system("PAUSE");
return 0;
}
float average (float a[5])
{
int sum = 0, i;
float total;
for(i=0; i<6; i++)
{
sum = sum+a[i];
}
total = sum/5;
return total;
}
Once you've fixed the type of sum as pointed out by bash.d, your answer will still be wrong. arr_1 contains 6 elements but you divide by 5 when calculating the average. You need to change the calculation of total to
total = sum/6;
Or, better still, change average to
float average(float* a, int num_elems)
{
float sum = 0
int i;
for(i=0; i<num_elems; i++) {
sum += a[i];
}
return sum/num_elems;
}
and call average like
total_avg = average (arr_1, sizeof(arr_1)/sizeof(arr_1[0]));
Why use int as sum? Use float instead:
float average (float a[5])
{
int sum = 0,
float total, sum = 0.0f;
for(i=0; i<6; i++)
{
sum = sum+a[i];
}
total = sum/5;
return total;
}

Resources