Вы находитесь на странице: 1из 21

Arrays, Adresses, pointer, Makros

1. Write a function dotprod which has two float arrays a, b of fixed length N as
input parameters and returns the scalar product (PLEASE CHECK GERMEN
VERSION OF SHEET05)s = (summ)from i = 0 to N-1 * a [i] b [i]. In this context, N
is defined as a macro in the framework program.

#include<stdio.h>

#include<conio.h>

#define N 10

float dotprod(float a1[],float b1[]);

void main()

float a[N],b[N];

float k;

int i,j;

clrscr();

printf("Enter the values of a:");

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

a[i]=0;

b[i]=0;

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

scanf("%f",&a[i]);

printf("Enter the values of b:");

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

{
scanf("%f",&b[i]);

k=dotprod(a,b);

printf("\nThe scalar product of a and b is %f",k);

getch();

float dotprod(float a1[],float b1[])

float scalar=0.00;

int j;

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

scalar=scalar+(a1[j]*b1[j]);

return scalar;

}
2. Write a function maxelement that has a fixed-length float array N as an input
parameter and returns the largest-value element

#include<stdio.h>

#include<conio.h>

void main()

float k,a[100];

int s,i;

clrscr();

printf("Enter the number of elements in array\n");

scanf("%d",&s);

printf("Enter %d integers\n",s);

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

scanf("%f", &a[i]);

k=maxelement(a,s);

printf("Maximum element is %f\n",k);

getch();

int maxelement(float ar[],int size)

float max=ar[0];

int i;

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

if(ar[i]<ar[i+1])

max=ar[i+1];

}
}

return max;

3. Write a function"crossprod" that has three pointers of a fixed-length float


arrays a, b, c as the input parameter, and writes the cross product of a and b in
c.
C = a b = ( a1 a2 a3)^T x ( b1 b2 b3 )^T = ( a2b3 -a3b2 a3b1 -a1b3 a1b2
-a2b1 )^T.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

float crossprod(float a1[],float b1[]);

float main()

float *ans,*first,*second;

float *A,*B,*C;

float i,j,k=0;

float rowA,colA,sizeA,sizeB,sizeC;

float rowB,colB;

prfloatf("Enter the row's and column of 1st matrix\n");

scanf("%d%d",&rowA,&colA);

prfloatf("Enter the row's and column of 2nd matrix\n");

scanf("%d%d",&rowB,&colB);

if(colA!=rowB)

prfloatf("Error => colA must be equal to rowB\n");

getch();

exit(EXIT_SUCCESS);
}

k=dotprod(a,b);

printf("\nThe scalar product of a and b is %f",k);

getch();

float crossprod(float a[],float b[])

sizeC = rowA*colB;

sizeA = rowA*colA;

sizeB = rowB*colB;

A = (float *)malloc(sizeA*sizeof(float *));

first = A;

B = (float *)malloc(sizeB*sizeof(float *));

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

prfloatf("\nThe value of matrix 'C' = \n");

ans = C;

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

prfloatf("\n");

for(j=0;j<colB;j++,ans++)

prfloatf("%d\t",*ans);

free(A);
free(B);

free(C);

getch();

4. Write a swap function that gets two pointers to int variables a, b, and swaps
the contents of a and b.

#include<stdio.h>

#include<conio.h>

void swap(int *a,int *b);

void main()

int a,b;

printf("Enter a and b respectively: ");

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

printf("Value before swapping:\n");

printf("a = %d \nb = %d \n",a,b);

swap(&a, &b);

printf("Value after swapping:\n");

printf("a = %d \nb = %d \n",a, b);

getch();

void swap(int *a,int *b)

int temp;

temp = *a;

*a = *b;
*b = temp;

}
5. Write a program in which an int32_t variable x is declared and the contents of
the following four bytes are output as an int variable with printf.

#include<stdio.h>

#include<conio.h>

void main()

long x;

int i;

printf("Enter the value:\n");

scanf("%ld",&x);

for (i=0;i<sizeof(x);i++)

unsigned char byte = *((unsigned char *)&x + i);

printf("Byte %d = %d\n", i, (int)byte);

getch();

}
6. Write a program for the multiplication of two matrices A ? R^(k, m) and B?
R^(m, n), where k, m, n?N are fixed. Test your program using the example
(PLEASE CHECK GERMEN VERSION OF SHEET05)A =( 1 2 3

4 5 6 ),

B = ( -1 0 1 4

0 1 -1 -3

2 1 3 0 ).

#include <stdio.h>

#include<conio.h>

void main()

{
int m, n, p, q, c, d, k, sum = 0;

int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");

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

printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)

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

scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");

scanf("%d%d", &p, &q);

if (n != p)

printf("Matrices with entered orders can't be multiplied with each other.\n");

else

printf("Enter the elements of second matrix\n");

for (c = 0; c < p; c++)

for (d = 0; d < q; d++)

scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++) {

for (k = 0; k < p; k++) {

sum = sum + first[c][k]*second[k][d];


}

multiply[c][d] = sum;

sum = 0;

printf("Product of entered matrices:-\n");

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++)

printf("%d\t", multiply[c][d]);

printf("\n");

getch();

}
7. Write a program in which an int array arr of length 5 is de fi ned. The fields of
arr are to be filled and output with their addresses converted to int. What can
you see at the output?

#include<stdio.h>

#include<conio.h>

void main()

int a[5];

int i;

clrscr();

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

scanf("%d",a[i]);

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

printf("%d",int(&a[i]));

getch();

8. WriteFunction ,"findmin" that has an Int-Array of length N as input parameter


and returns the address of the least-valued element.

#include<stdio.h>

#include<conio.h>

void main()

int a[100];

int s,i;

int k;

clrscr();
printf("Enter the number of elements in array\n");

scanf("%d",&s);

printf("Enter %d integers\n",s);

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

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

k=findmin(a,s);

printf("Address of the minimum element is %ld\n",k);

getch();

int findmin(int ar[],int size)

int min=ar[0];

int i;

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

if(ar[i]>ar[i+1])

min=(int)&ar[i+1];

return min;

}
9. Write a functioncycle3 that has , the pointer to the3 variables a, b, c,as
parameter and cyclically interchanges the contents of these variables. Example:
a = 6, b = 4, c = 5 should have the values a = 5, b = 6, c = 4 according to the
function.

#include<stdio.h>

#include<conio.h>

void functioncycle3(int *a,int *b,int *c);


void main()

int a,b,c;

printf("Enter a, b and c respectively: ");

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

printf("Value before swapping:\n");

printf("a = %d \nb = %d \nc = %d\n",a,b,c);

functioncycle3(&a, &b, &c);

printf("Value after swapping:\n");

printf("a = %d \nb = %d \nc = %d",a, b, c);

getch();

void functioncycle3(int *a,int *b,int *c)

int temp;

temp = *b;

*b = *a;

*a = *c;

*c = temp;