Week 5 Coding Assignment Name: Priyanka Indra Roll No.: 84 Dept: CSE1 Sem: 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

WEEK 5 CODING ASSIGNMENT

Name: Priyanka Indra Roll No.: 84 Dept: CSE1 Sem: 6th

Program 1:
Given two arrays of n numbers each, form a pair of numbers taking one number from array1 and
the other from array2 such that the sum of the pair of this numbers is the maximum possible for
the given arrays. Take input from STDIN. Don’t use any sorting technique.

Input:
2 1 5 7 10
36812

Output:
18

Source Code:
#include<stdio.h>
int main(void) {
int n,i,a[10],b[10],a_max,b_max,sum;
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter the elements of 1st array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the elements of 2nd array: ");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
a_max=a[0];
b_max=b[0];
for(i=0;i<n;i++) {
if(a_max<a[i])
a_max=a[i];
if(b_max<b[i])
b_max=b[i];
}
sum=a_max+b_max;
printf("Sum: %d",sum);
return 0;
}

Output:
Enter number of elements: 5
Enter the elements of 1st array: 2 1 5 7 10
Enter the elements of 2nd array: 3 6 8 1 2
Sum: 18

Program 2:
Display Pascal Triangle pattern. Pascal’s triangle is a triangular array of the binomial
coefficients. Write a function that takes an integer value n as input from command line argument
and prints first n lines of the Pascal’s triangle. Optimize the time and space complexity.

Test Cases:
-----------
1. VALID INPUT:
a) Only one argument will be given as input.
2. INVALID inputs:
a) Two or more than two arguments.
b) Characters other than digits.
3. You should generate output as follows:
a) Display pattern to the STDOUT without any additional text.
b) If error print 'ERROR' to the STDOUT without any additional text

Source Code:
#include<stdio.h>
void printPascal(int n) {
int i,c,j;
for(i=1;i<=n;i++) {
c=1;
for(j=1;j<=i;j++) {
printf("%d ", c);
c=c*(i-j)/j;
}
printf("\n");
}
}
int main() {
int n;
printf("Enter number of lines: ");
scanf("%d",&n);
printPascal(n);
return 0;
}

Output:
Enter number of lines: 5
1
11
121
1331
14641

Program 3
Given an array of elements of length N, ranging from 1 to N. All elements may not be present in
the array. If element is not present then there will be -1 present in the array. Rearrange the array
such that
A[i] = i and if i is not present, display -1 at that place. Take input from STDIN.

Examples:
Input : arr = {-1, -1, 6, 1, 9, 3, 2, -1, 4, -1}
Output : [-1, 1, 2, 3, 4, -1, 6, -1, -1, 9]
Input : arr = {19, 7, 0, 3, 18, 15, 12, 6, 1, 8, 11, 10, 9, 5, 13, 16, 2, 14, 17, 4}
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Source Code:
#include<stdio.h>
int main(void) {
int n,i,a[30],j,pos,f=0;
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter the elements of 1st array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++) {
f=0;
for(j=0;j<n;j++) {
if(a[j]==i) {
f=1;
pos=j;
break;
}
}
if(f==1)
printf("%d ",a[pos]);
else
printf("-1 ");
}
return 0;
}

Output (1):
Enter number of elements: 10
Enter the elements of 1st array: -1 -1 6 1 9 3 2 -1 4 -1
-1 1 2 3 4 -1 6 -1 -1 9

Output (2):
Enter number of elements: 20
Enter the elements of 1st array: 19 7 0 3 18 15 12 6 1 8 11 10 9 5 13 16 2 14 17 4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Program 4
Write a program count the number of vowels from the string. Convert it from lower case to upper
case and then reverse the string.
Examples:
Input:
Information
Output:
5
INFORMATION
NOITAMROFNI

Test Cases:
-----------
1. VALID INPUT:
a) Only one argument will be given as input.
2. INVALID INPUTS:
a) No argument
b) Two or more than two arguments.
c) Characters other than alphabet.
3. You should generate output as follows:
a) Print to the STDOUT without any additional text.
b) If error print 'ERROR' to the STDOUT without any additional text

Source Code:
#include<stdio.h>
#include<string.h>
int main(void) {
int i,vol=0,l;
char s[15],ch;
printf("Enter the word: ");
scanf("%s",s);
for(i=0;s[i]!='\0';i++) {
if(s[i]=='a'|| s[i]=='e'|| s[i]=='i'|| s[i]=='o'|| s[i]=='u')
vol++;
}
printf("Vowels: %d\n",vol);
for(i=0;s[i]!='\0';i++) {
s[i]=s[i]-32;
}
printf("UpperCase: %s\n",s);
l=strlen(s);
for(i=0;i<=l/2;i++) {
ch=s[i];
s[i]=s[l-1-i];
s[l-1-i]=ch;
}
printf("Reverse: %s",s);
return 0;
}

Output:
Enter the word: information
Vowels: 5
UpperCase: INFORMATION
Reverse: NOITAMROFNI

Program 5
C Program to Copy a File into another File. Take file name from command line argument.

Test Cases:
-----------
1. VALID INPUT:
a) Only two argument will be given as input.
2. INVALID INPUTS:
a) No argument
b) More than two arguments.

Source Code:
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[]) {
FILE *mf;
FILE *cf;
char ch;
if(argc!=3) {
printf("Invalid argument");
exit(1);
}
mf=fopen(argv[1],"r");
cf=fopen(argv[2],"w");
if(mf==NULL || cf==NULL) {
printf("File not found");
exit(1);
}
ch=fgetc(mf);
while(ch!=EOF) {
fputc(ch,cf);
ch=fgetc(mf);
}
printf("\nFile Copied Successful");
fclose(mf);
fclose(cf);
return 0;
}
Output:
gcc FileCopy.c input.txt output.txt
File Copied Successful.

You might also like