Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 24

Advanced C Programming

Theory Digital Assignment -1

Problem 1

A) The curly braces in C defines a block. A block is a section of codes


which isolates its variables and statements from rest of the code. It
makes sense to use curly braces to surround body of a function
because function is an independent module of code which is used to
improve reusability of code, curly braces perform a similar operation.
B) 7 - is an integer literal (L-value) , “7” - is a string literal and ‘7’ - is a
character type literal
C) double ans = 10.0+(2.0/((3.0-2.0)*2.0));

Problem 2

Order of line numbers is : 6,4,5,2,7,1,3

Problem 3

#include <stdio.h>
#include <stdlib.h>

#define SPACED_LINE "* *"


int main(){
for(int i =0; i<9; i++){
printf("*");
}
printf("\n");
for(int i = 0; i<9; i++){
printf(SPACED_LINE);
printf("\n");
}
for(int i =0; i<9; i++){
printf("*");
}
printf("\n");
}
Problem 4

#include <stdio.h>
#include <stdlib.h>

#define FIVE_LAKHS 500000.0

double pow(double base, double exp)


{ double ans = 1.0;
for (int i =0; i<exp; i++){
ans *= base;
}
return ans;
}

double get_compound_interest(double p, double r, double n, double t)


{ double amount = p*(pow(1+(r/n),n*t));
return amount-p;
}

double get_simple_interest(double p, double r, double t)


{ double interest = (p*r*t)/100;
return interest;
}

int main(void){
double principal = FIVE_LAKHS;
double years = 10.0;
double rate = 3.5/100;

printf("Simple interest on Rs. 500000.00 in 10 years = Rs. %.2f


\n" ,get_simple_interest(principal,rate,years));
printf("Interest on Rs. 500000.00 in 10 years compounded anually = Rs.
%.2f \n"
,get_compound_interest(principal,rate,1,10));
printf("Interest on Rs. 500000.00 in 10 years compounded semi-anually
= Rs. %.2f \n"
,get_compound_interest(principal,rate,2,10));
printf("Interest on Rs. 500000.00 in 10 years compounded quarterly =
Rs. %.2f \n"
,get_compound_interest(principal,rate,4,10));
printf("Interest on Rs. 500000.00 in 10 years compounded monthly =
Rs. %.2f \n"
,get_compound_interest(principal,rate,12,10));
printf("Interest on Rs. 500000.00 in 10 years compounded daily =
Rs. %.2f \n"
,get_compound_interest(principal,rate,365,10));

return 0;
}
Problem 5

1. %2d implies the minimum width the output should be i.e. 2 units. If the
width of the output is less than 2 units, blank spaces will preceed the output
as filler. In our case the output length is greater than two so the full output
will be printed.

2. %10.2d implies the minimum width the output should be i.e. 10 units. If
the width of the output is less than 10 units, blank spaces will preceed the
output as filler. The formatting .2 implies the minimum length of the output
without the filler, preeceding zeros will be used to attain this. In our case
the output length is 5 so 5 more spaces will be printed before the actual
output to make the total length 10.

3. %-10.2d implies the minimum width the output should be i.e. 10 units.
The output should be left aligned ( because – sign is used ). If the width of
the output is less than 10 units, blank spaces will succeed the output as
filler. The formatting .2 implies the minimum length of the output without
the filler, preeceding zeros will be used to attain this. In our case the output
length is 5 so 5 more spaces will be printed after the actual output to make
the total length 10.
4. %-7d implies the minimum width the output should be i.e. 7 units. The
output should be left aligned ( because – sign is used ). If the width of the
output is less than 7 units, blank spaces will succeed the output as filler. In
our case the output length is 3 so 4 more spaces will be printed after the
actual output to make the total length 7.

5. %07.2d implies the minimum width the output should be i.e. 7 units. If
the width of the output is less than 7 units, blank spaces will preceed the
output as filler. The formatting .2 implies the minimum length of the output
without the filler, preeceding zeros will be used to attain this. In our case
the output length is 3 so 4 more spaces will be printed before the actual
output to make the total length 7.

6. %07d implies the minimum width the output should be i.e. 7 units. Zeros
will be used as filler instead of blank spaces ( because 0 is specifically
mentioned in the placeholder ). If the width of the output is less than 7
units, zeros will preceed the output as filler. In our case the output length is
3 so 4 zeros will be printed before the actual output to make the total length
7.

7. %+0-9.4d implies the minimum width the output should be i.e. 9 units. A
sign bit will preceed the output. The formatting .4 implies the minimum
length of the output without the filler, preeceding zeros will be used to attain
this. The output should be left aligned ( because – sign is used ). If the
width of the output is less than 9 units, blank spaces will succeed the
output as filler. In our case ‘+0123 ‘ will be printed.

8. %+09.4d implies the minimum width the output should be i.e. 9 units. A
sign bit will preceed the output. The formatting .4 implies the minimum
length of the output without the filler, preeceding zeros will be used to attain
this. If the width of the output is less than 9 units, blank spaces will preceed
the output as filler. In our case ‘ +0123‘ will be printed.

9. %+07d implies the minimum width the output should be i.e. 7 units. A
sign bit will preceed the output. Zeros will be used as filler instead of blank
spaces ( because 0 is specifically mentioned in the placeholder ). If the
width of the output is less than 7 units, zeros will preceed the output as
filler. In our case ‘+000123‘ will be printed.

10. similar to no. 8.


11. %+-07.4d implies the minimum width the output should be i.e. 7 units.
A sign bit will preceed the output. The formatting .4 implies the minimum
length of the output without the filler, preeceding zeros will be used to attain
this. The output should be left aligned ( because – sign is used ). If the
width of the output is less than 7 units, blank spaces will succeed the
output as filler. In our case ‘+0123 ‘ will be printed.

12. similar to no. 4.

13. similar to no. 5 with left align.

14. similar to no. 13.

Problem 6
Problem 7

Problem 8
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void){
double x;
printf("Enter the value of x : ");
scanf("%lf",&x);
if(x==0.0){
printf("Value of x must be nonzero: try again\n");
return 0;
} else {
printf("Value of sin(1/x) is %.4lf\n",sin(1.0/x));
}
return 0;
}

Problem 9

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

char* strrev(char* st){


int lp = 0, rp = strlen(st)-1;
while(lp<rp){
char tmp = st[lp];
st[lp] = st[rp];
st[rp] = tmp;
lp+=1;
rp-=1;
}
return st;
}

char* to_binary(int n){


char* bin = (char*)
malloc(7*sizeof(char)); while(n){
if (n&1){
strcat(bin,"1");
} else
{ strcat(bin,"0"
);
}
n = n>>1;
}
return strrev(bin);
}
int main(void){
int n;
printf("Enter a +ve no less than 32: ");
scanf("%d",&n);
if(n>=0 && n<32){
printf("Binary equivalent of decimal %d is
%s\n",n,to_binary(n)); } else {
printf("Entered number is out of range\n");
}
return 0;
}

Problem 10

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef long long int ll;
ll get_series_sum(int n){
ll ans = 0l;
ll cnt = 0ll;
for(ll i = 1l; i<=(ll)n; i+=cnt)
{ ans += (ll)pow(i,4l); +
+cnt;
}
return ans;
}
int main(void){
int n;
printf("Enter a +ve integer less than 50: ");
scanf("%d",&n);
if(n>=1 && n<50){
printf("Sum of series is
%lld\n",get_series_sum(n)); } else {
printf("Invalid input\n");
}
return 0;

}
Problem 11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef long long int ll;


int min(int a, int b){
if (a<b){
return a;
}
return b;
}

int max(int a, int b){


if(a>b){
return a;
}
return b;
}

int main(void){
int n, mini = INT32_MAX, maxi = INT32_MIN, positives = 0, sum =
0; float avg = 0.0f;

while(1){
printf("Enter a +ve integer: ");
scanf("%d",&n);
if(n<=0){
if (positives==0){
printf("No positive number entered\n");
return 0;
}
printf("Number of +ve values entered is
%d\n",positives); printf("Maximum value entered is
%d\n",maxi); printf("Minimum value entered is
%d\n",mini); printf("Average value is %.4f\n",avg); return
0;
} else { positives++;
mini =
min(mini,n);
maxi = max(maxi,n);
sum += n;
avg = ((float)sum)/((float)positives);
}
}
return 0;

}
Problem 12

Problem 13

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef long long int ll;

int main(void){
ll salary;
long double ans = 0LL;
printf("Enter salary : ");
scanf("%lld",&salary);

if (salary>=1ll && salary<=4000ll){


ans = salary + ((10.0/100.0)*salary) + ((50.0/100.0)*salary);
printf("Salary is %.2Lf \n",ans);

}
else if (salary>=4001ll && salary<=8000ll){
ans = salary + ((20.0/100.0)*salary) + ((60.0/100.0)*salary);
printf("Salary is %.2Lf \n",ans);
}
else if (salary>=8001ll && salary<=12000ll){
ans = salary + ((25.0/100.0)*salary) + ((70.0/100.0)*salary);
printf("Salary is %.2Lf \n",ans);

}
else if (salary>12000ll){
ans = salary + ((30.0/100.0)*salary) + ((80.0/100.0)*salary);
printf("Salary is %.2Lf \n",ans);

} else {
printf("Invalid salary \n");
}
return 0;
}
Problem 14

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef long long int ll;

void print_array(int* arr, int n){


for(int i = 0; i<n; i++){
printf("%d ",arr[i]);
}
printf("\n");
}

void permute(char* curr, char* original, int* processed)


{ if(strlen(curr)==strlen(original)){
printf("%s\n",curr);
free(curr);
return;
}
for(int i = 0; i<strlen(original); i++){
if(processed[i]==0){
char* new_str = (char*) malloc((strlen(original)
+1)*sizeof(char)); strcpy(new_str,curr);
processed[i]=1;
char addup[2];
addup[0] = original[i]; addup[1] = '\0';
strcat(new_str,addup);
permute(new_str,original,processed);
processed[i]=0;
}
}
}

int main(void){
char* str = "MONKEY";
int processed[strlen(str)];
for(int i = 0; i<strlen(str); i++){
processed[i]=0;
}
permute("",str,processed);
return 0;
}
Problem 15

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int** get_matrix(int rows, int cols){


int** matrix = (int**) malloc(rows*sizeof(int*));
for(int i =0; i<rows; i++){
matrix[i] = (int*) malloc(cols*sizeof(int));
}
return matrix;
}

void print_matrix(int** mat, int r, int c)


{ for(int i=0; i<r; i++){
for(int j =0; j<c; j++){
printf("%d ",mat[i][j]);
}
printf("\n");
}
}

void input_matrix(int** mat, int r, int c)


{ for(int i = 0; i<r; i++){
for(int j = 0; j<c; j++){
scanf("%d",&mat[i][j]);
}
}
}

void multiply(int** matrix_a, int** matrix_b, int ra, int ca, int rb, int cb)
{ if(ca!=rb){
printf("Can't multipy matrices, dimesions don't match \n");
return;
}
int** ans = get_matrix(ra,cb);
for(int i = 0; i<ra; ++i){
for(int j = 0; j<cb; ++j){
int k_sum = 0;
for(int k = 0; k<ca; ++k){
k_sum += (matrix_a[i][k]*matrix_b[k][j]);
}
ans[i][j] = k_sum;
}
}
print_matrix(ans,ra,cb);
}

int main(void){
printf("Enter dimensions for matrix A :
"); int ra,ca;
scanf("%d %d",&ra,&ca);
printf("Enter contents of matrix A \n");
int** matrix_a = get_matrix(ra,ca);
input_matrix(matrix_a,ra,ca);

printf("Enter dimensions for matrix B :


"); int rb,cb;
scanf("%d %d",&rb,&cb);
printf("Enter contents of matrix B \n");
int** matrix_b = get_matrix(rb,cb);
input_matrix(matrix_b,rb,cb);
printf("Resultatnt matrix is \n");

multiply(matrix_a,matrix_b,ra,ca,rb,cb);

return 0;
}

You might also like