Sei sulla pagina 1di 6

CSI 301 Programming Fundamentals

Department of Computer Science

ASSIGNMENT: 05

NAME: Mudassar Mubarak


CLASS: BS-CS
SUBMITTED TO: Sir Haris Ahmed
SUBMISSION DATE: 3rd Dec, 2019

CASHT University, Rawalpndi


QUESTION#01

Sum of Array

Pseudocode:
1) Initializing variable,sum,number
2) Asking for the elements from the user.
3) Using for loop to get the values from user.
4) Print values of Array.
5) Print sum.
6) Using again for loop.

Program:-
#include<stdio.h>

int main(){

int a[5],s,i,n;

printf("\n how many elements");

scanf("%d",&n);

printf("\n enter array elements");

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

printf("\n sum of array elements are");

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

s=s+a[i];
}

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

QUESTION#02
Maximum and minimum value in Array
Pseudocode:
1) Initializing varaibles,number,size of an array.
2) Entering size of an Array from user.
3) Entering element of Array from user.
4) Using for loop to get the values.
5) Assigning value of maximum and minimum.
6) Using again for loop

Program:-
#include<stdio.h>
int main()
{
int a[10], Size, i, Min, Max ;

printf("\n Enter the size of an array : ");


scanf("%d",&Size);
printf("\n Enter %d elements of an array: \n", Size);
for(i=0; i<Size; i++)
{
printf("\n Index of array[%d] is \n", i, &a[i]);
scanf("%d",&a[i]);
}
Min = a[0];
Max = a[0];
for(i=1; i<Size; i++)
{
if(Min > a[i])
{
Min = a[i];
}
if(Max < a[i])
{
Max=a[i];
}
}
printf("\n Min value in an Array = %d", Min);
printf("\n Max value in an Array = %d", Max
);
}
QUESTION#03
Index of searched value in Array
Pseudocode:
1) Initializing variables and constants.
2) Entering value of an Array from user.
3) Using for loop.
4) Inside loop check if current Array element is equal to searched or not.
5) Outside loop if(c==1)then element is founf otherwise not.

Program:-

#include <stdio.h>
int main()
{
int a[]={1,3,4,5,7,8,9,};
int num,i,p,c=0;
int n=sizeof(a)/sizeof(a[0]);

printf("Enter a value :\n");-


scanf("%d",&num);

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

if(a[i]==num)
{
p=i;
printf("Element index is %d",p);
c=1;
break;
}
else
{
printf("Element not in array:");
c=0;
}
}
}

Potrebbero piacerti anche