Assigment 2 solved
Powered by nalayk no 252 danish
Write
a program to create an array of 10 elements, randomly fill the array and then
print the sum of all elements.
#include
<stdio.h>
#include<stdlib.h>
#include<conio.h>
void
main (void){
randomize();
int
a[10];
int
sum=0;
for(int
i=0;i<10;i++)
a[i]=random(100);
for(i=0;i<10;i++){
printf("%d\n",a[i]);
sum+=a[i]; }
printf("The
Sum is : %d",sum); }
2
Write
a program that can fill an array of 10 elements by randomly generated Integers,
range (1-100). Sort the array by using bubble sort. (Read bubble sort from any
book or from net)
#include
<stdio.h>
#include<stdlib.h>
#include<conio.h>
void
main (void) {
randomize();
int
a[10];
int
temp=0;
printf("Unsorted
Random Values :\n\n");
for(int
i=0;i<10;i++){
a[i]=random(100);
printf("%d\n",a[i]);
}
for(i=0;i<10;i++)
for(int
j=0;j<9;j++)
if(a[j]>a[j+1]){
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
printf("\nSorted
Values :\n");
for(i=0;i<10;i++)
printf("%d\n",a[i]); }
3
Write
a program that can fill an array of 10 elements by randomly generates Integers,
range (1-100). Find the Minimum and Maximum values without sorting the array.
#include
<stdio.h>
#include<stdlib.h>
#include<conio.h>
void
main (void) {
randomize();
int
a[10];
int
min=0;
int
max=0;
int
minIndex=0;
int
maxIndex=0;
printf("Unsorted
Random Values :\n");
for(int
i=0;i<10;i++){
a[i]=random(100);
printf("%d\n",a[i]);
}
min=a[0];
max=a[0];
for(i=1;i<10;i++){
if(a[i]<min){
min=a[i];
minIndex=i;
}
if(a[i]>max){
max=a[i];
maxIndex=i;
} }
printf("\nThe
Minimum Value is : %d at Index Number : %d\n",min,minIndex);
printf("The
Maximum Value is : %d at Index Number : %d",max,maxIndex); }
4
Create
a two-dimension array randomly fill it, print the array and the sum of all the
elements.
#include
<stdio.h>
#include<stdlib.h>
#include<conio.h>
void
main (void) {
randomize();
int
a[3][5];
int
sum=0;
for(int
i=0;i<3;i++)
for(int
j=0;j<5;j++)
a[i][j]=random(100);
for(i=0;i<3;i++){
for(int
j=0;j<5;j++){
sum+=a[i][j];
printf("%d
",a[i][j]); }
printf("\n");
}
printf("\nThe
Sum of All Values is : %d \n",sum); }
5
Create
a three-dimension array randomly fill it print the array and the sum of all
elements.
#include
<stdio.h>
#include<stdlib.h>
#include<conio.h>
void
main (void) {
randomize();
int
a[3][4][5];
int
sum=0;
for(int
i=0;i<3;i++)
for(int
j=0;j<4;j++)
for(int
k=0;k<5;k++)
a[i][j][k]=random(100);
for(i=0;i<3;i++){
for(int
j=0;j<4;j++){
for(int
k=0;k<5;k++){
sum+=a[i][j][k];
printf("%d
",a[i][j][k]);}
printf("\n");}
printf("\n");}
printf("\nThe
Sum of All Values is : %d \n",sum); }
6
Write
a program that can take 4 integers values as input. Pass all values to a
function sum. Function sum will call another function average and pass first 2
values; average will calculate the average and return the result to sum. Sum
will pass remaining two values to average and after getting the result, add
them and return the result to main function for printing.
#include
<stdio.h>
#include<stdlib.h>
#include<conio.h>
int
sum(int,int,int,int);
int
average(int,int);
void
main (void) {
int
number1,number2,number3,number4;
printf("Enter
First Number : ");
scanf("%d",&number1);
printf("Enter
Second Number : ");
scanf("%d",&number2);
printf("Enter
Third Number : ");
scanf("%d",&number3);
printf("Enter
Fourth Number : ");
scanf("%d",&number4);
printf("The
Sum of the Average is : %d",sum(number1,number2,number3,number4)); }
int
sum(int number1,int number2,int number3,int number4){
int
a,b;
a=average(number1,number2);
b=average(number3,number4);
return
a+b; }
int
average(int number1,int number2){
int
avr;
avr=(number1+number2)/2;
return
avr; }
7
Write
a program that can take three integer values as input. Use a loop to generate a
table of first value starting from the second and ending at the third value.
The printing of the table will be performed by print macro in standard format
i.e.
(2 * 2 = 4).
#include
<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define
PN(number1,number2) printf("%d * %d =
%d\n",number1,number2,number1*number2);
void
main (void) {
int
number1,number2,number3;
printf("Enter
First Number : ");
scanf("%d",&number1);
printf("Enter
Second Number : ");
scanf("%d",&number2);
printf("Enter
Third Number : ");
scanf("%d",&number3);
for(;number2<=number3;number2++)
PN(number1,number2)
}
8
Write
a program that can create an array of 10 elements type integer, randomly fill
the array and then pass the array to a function sum. Sum will add values of
array and return the result to main function for printing. (Add a 2 and a 3
dimension array in the program, use [3][5] and [2][4][7] values for array
definitions. Fill both arrays randomly and pass them to sum2 and sum3 functions
for additional. The results will be printed by the main function)
#include
<stdio.h>
#include<stdlib.h>
#include<conio.h>
int
sum1(int[]);
int
sum2(int[][5]);
int
sum3(int[][4][5]);
void
main (void) {
randomize();
int
a[10];
int
b[3][5];
int
c[3][4][5];
//Single
Dimension Array filled with Random Numbers
for(int
i=0;i<10;i++)
a[i]=random(100);
//Two
Dimension Array filled with Random Numbers
for(i=0;i<3;i++)
for(int
j=0;j<5;j++)
b[i][j]=random(100);
//Three
Dimension Array filled with Random Numbers
for(i=0;i<3;i++)
for(int
j=0;j<4;j++)
for(int
k=0;k<5;k++)
c[i][j][k]=random(100);
printf("The
Sum of One Dimension Array is :
%d\n",sum1(a));
printf("The
Sum of Two Dimension Array is : %d\n",sum2(b));
printf("The
Sum of Three Dimension Array is : %d\n",sum3(c)); }
int
sum1(int a[]){
int
sum=0;
for(int
i=0;i<10;i++)
sum+=a[i];
return
sum; }
int
sum2(int b[][5]){
int
sum=0;
for(int
i=0;i<3;i++)
for(int
j=0;j<5;j++)
sum+=b[i][j];
return
sum; }
int
sum3(int c[][4][5]){
int
sum=0;
for(int
i=0;i<3;i++)
for(int
j=0;j<4;j++)
for(int
k=0;k<5;k++)
sum+=c[i][j][k];
return
sum; }
MATRICES
2X3 Matrix
#include<stdio.h>#include<conio.h>
void main(void)
{
int test[3][2],a,b;
clrscr();
printf("\t\tEnter your 2x3 column");
for(b=0;b<2;b++)
{
printf("\n");
for(a=0;a<3;a++)
{
printf("\nEnter a number on this position %d%d = ",b,a);
scanf("%d",&test[a][b]);
}
}
printf("\n\nHere is your Transpose\n\n");
for(a=0;a<3;a++)
{
printf("\n");
for(b=0;b<2;b++)
{
printf("\t");
printf("%d",test[a][b]);
}
getch();
}
3x2 Matrix
#include<stdio.h>
#include<conio.h>
void main(void)
{
int test[3][2],a,b;
clrscr();
printf("\t\tEnter your 2x3 column");
for(a=0;a<2;a++)
{
for(b=0;b<3;b++)
{
printf("\nEnter a number on this position %d%d = ",a,b);
scanf("%d",&test[a][b]);
}
}
printf("\n\nHere is your Transpose\n\n");
for(a=0;a<2;a++)
{
printf("\n");
for(b=0;b<3;b++)
{
printf("\t");
printf("%d",test[a][b]);
}
}
getch();
}