You are on page 1of 10

Sheet 12

2-D Arrays, Struktures, Funktionspointer, statik Variable

1]Write a program that multiplies a vector of length n with a matrix m × n, and
stores the result vector in a text file as a column vector. The matrix, the vector
should be read from the keyboard.

#include <stdio.h>

void main()

{

int n = 4;

int m1[4],i;

int m2[4][4] = {

{1,2,3,4},

{5,6,7,8},

{9,10,11,12},

{13,14,15,16}

};

int multiplied_matrix[4][1] = {

{0},

{0},

{0},

{0}

};

for(i=0;i<n;i++)

scanf("%d",&m1[i]);

void matrix_x_vector(int n, int y[n], int x[n][n], int A[n][n]);

matrix_x_vector(n, m1, m2, multiplied_matrix);
printf("\n");

}

void matrix_x_vector(int n, int y[n], int x[n][n], int A[n][n])

{FILE *FP=fopen("vec.txt","w");

int i, j;

printf("\nResulted Matrix of [M]*[V]\n");

for (i=0; i<n; i++)

{

for (j=0; j<n; j++)

{

A[j][0] += x[j][i] * y[i];

f printf(fp,"%4i", A[j][0]);

}

printf("\n");

}

printf("\n");

for (i=0; i<n; i++)

fprintf(fp,"%4i\n", A[i][0]);

fclose(fp);

}

2] What does the keyword static mean? Write a function that increases an internal
counter by one each time and retains its value until the next function call. Prior to
the increase of the counter, the old counter value should be saved in a variable and
returned at the end of the function. Write a main program, in which the function is
called multiple times and the value is returned on the screen.
static is used with global variables and functions to set their scope to the containing
file. In local variables, static is used to store the variable in the statically allocated
memory instead of the automatically allocated memory.

#include<stdio.h>

int fun()

{

static int count = 0;

count++;

return count;

}

int main()

{

printf("%d ", fun());

printf("%d ", fun());

return 0;

}

3] write a function func with two integer input parameters and one double
returnvalue. What the function func does is not relevant. Write a main program to
the function func deklare a suitable pointer for func and allocate to the pointer,the
function func . Use only the pointer for a function call.

#include <stdio.h>

// #include <conio.h>

double (*fun_pntr)(int, int);
double func(int m, int n){

return (m/n);

}

void main()

{

int a,b;

double ans;

fun_pntr = &func;

printf("Enter any two numbers\n");

scanf("%d%d",&a,&b);

ans=(*fun_pntr)(a,b);

printf("The answer is %f\n", ans);

}

4] define one structure for the management of participants of a seminar. The
following data of the participants should be recorded: the first and last name, full
address (see below), the account data and their respective roomnumber in the
hotel. To store the address of the participant you define another structure with the
following items: country, postal code, city, street and house number. Now, write a
program that reads the data of the participants in an infinite loop and stores it in a
text file.

#include<stdio.h>

#include<conio.h>

struct pardetail

{

char firname[30];

char secname[30];

int accno;
int roomno;

}

struct address

{

char country[20],city[30],street[50;

int pscd,houseno;

}

int main()

{

struct pardetail det;

struct address addr;

char ch;

int chi=1;

FILE *fp=fopen("detail.txt","w");

while(chi==1)

{ printf("enter your first name");

scanf("%s",&det.firname);

printf("enter your sec name");

scanf("%s",&det.secname);

printf("enter accno and roomno");

scanf("%d %d",&det.accno,&det.roomno);

printf("enter your adress country ,city, street,housenumber,postal code in the
above order");

scanf("%s%s%s%d
%d",&addr.country,&addr.city,&addr.street,&addr.houseno,&addr.pscd");
fprintf("firstname: %s secondname: %s accno: %d roomno: %d country: %s
city: %s street: %s houseno:%d postalcode:
%d",det.firname,det.secname,det.roono,addr.country,addr.city,addr.street,addr.hous
eno,addr.pscd);

printf("do you want to continue y or n");

scanf("%c",ch);

if(ch=='y')

{

chi=1;

}

else

{

chi=0;

}}

}

5] defining one global pointer-Data type to a function with a double-inputparameter
and double-return value. Write a function slope, which contains this Pointer-Datatyp
and 2 other inputparameters a and b . The function slope should now calculate the
slope of the secant of the function that is passed on ,at the given points a and b.
Test its function, by writing a main program which calculates and displays the slope
of the sekants of the cos function which passes through the points a=0.0 and b=0.5
.

#include<stdio.h>

#include<conio.h>

#include<math.h>

double * slope(double a1,double b1)
{double val;

val =(cos(b1)*cos(a1))/(b1-a1);

return val;

}

void main()

{

double a=0.0,b=0.5;

double *y;

y=slope(a,b);

printf("the slope of the value is %lf",y);

}

6] write a program which reads two real matrices of A ? RA × b and B ? RC × d of
any dimension a,b,c,d, calculates their Kronecker product, and displays(the output).
The allocated memory is to be freed again. Test your implementation on the
following example:(Please check the Germanversion sheet 12)

#include <stdio.h>

#include <stdlib.h>

// #include <conio.h>
int get_matrix(){

int **mat,m,n;

printf("Enter the Dimensions for the Matrix\n");

scanf("%d%d",&m,&n);

mat = malloc(m * sizeof(int *));

for(int i = 0; i < m; i++)

{

mat[i] = malloc(n * sizeof(int));

}

printf("Enter the elements of the Matrix one by one\n");

for (int i = 0; i < m; i++)

{

for (int j = 0; j < n; j++)

{

printf("Enter the Element for [%d][%d]\n",i,j);

scanf("%d",&mat[i][j]);

}

}

return **mat;

}

void display(int **mat){

int row=(sizeof(mat)/sizeof(mat[0]));

int col=(sizeof(mat)/sizeof(mat[0][0]))/row;

for (int i = 0; i < row; i++)

{

for (int j = 0; j < col; j++)
{

printf("%d\n",mat[i][j]);

}

printf("\n");

}

}

int Kronecker(int **a,int **b){

int **ans;

int a_row=(sizeof(a)/sizeof(a[0]));

int a_col=(sizeof(a)/sizeof(a[0][0]))/a_row;

int b_row=(sizeof(b)/sizeof(b[0]));

int b_col=(sizeof(b)/sizeof(b[0][0]))/b_row;

for (int i = 0; i < a_row; i++)

{

for (int j = 0; j < a_col; j++)

{

for (int k = 0; k < b_row; k++)

{

for (int l = 0; l < b_col; l++)

{

ans[i][j] = a[i][j]*b[k][l];

}

}

}

}

return **ans;
}

void free_mat(int **mat){

int row=(sizeof(mat)/sizeof(mat[0]));

for(int i = 0; i < row; i++)

free(mat[i]);

free(mat);

}

void main(){

int **a,**b,**ans;

a = get_matrix();

b = get_matrix();

display(a);

display(b);

printf("Executing Kronecker......\n");

ans = Kronecker(a,b);

free_mat(a);

free_mat(b);

}