Random File Access in C

WAP to print the first character of each word from the string contained in a file “details.txt”. The file “details.txt” has “Deep Raj Bhujel” string in it and your program should print “D R B” on the console screen. [Random File Access: fseek(), ftell(), rewind()]

#include<stdio.h>

#include<stdlib.h>

int main()

{

    FILE *fp;

    char ch;

    //long pos;


    fp = fopen("details.txt","r");


    if(fp == NULL)

    {

        puts("Error opening file");

        exit(1);

    }


    //printf("%ld ",ftell(fp));

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    fseek(fp,4,SEEK_CUR);

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    fseek(fp,3,SEEK_CUR);

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    //rewind(fp);

    //printf("%ld ",ftell(fp));


    return 0;

}


Caesar Cipher in C

 #include<stdio.h>

int main(){

char inputtext[]="Deep Raj Bhujel", ciphertext[100], decipheredtext[100];

int i,shift=3;

for(i=0;inputtext[i]!='\0';i++){

if(isupper(inputtext[i]))

ciphertext[i]=(inputtext[i]-65+shift)%26 + 65;

else if(islower(inputtext[i]))

ciphertext[i]=(inputtext[i]-97+shift)%26 + 97;

else

ciphertext[i]=inputtext[i];

}

ciphertext[i]='\0';

printf("The encrypted string is %s\n\n",ciphertext);

for(i=0;ciphertext[i]!='\0';i++){

if(isupper(ciphertext[i]))

decipheredtext[i]=(ciphertext[i]-65-shift)%26 + 65;

else if(islower(ciphertext[i]))

decipheredtext[i]=(ciphertext[i]-97-shift)%26 + 97;

else

decipheredtext[i]=ciphertext[i];

}

decipheredtext[i]='\0';

printf("The decrypted string is %s",decipheredtext);

return 0;

}

Implementation of Preprocessor in C Programming

Q. Write macro definitions with arguments for calculation of area and perimeter of a triangle, a square and a circle. Store these macro definitions in a file called "areaperi.h". Include this file in your program, and call the macro definitions for calculating area and perimeter for different triangles, squares and circles.

Step 1: Create a header file (.h) declaring the prototypes of required functions.

float area1(int, int, int); // for triangle
int area2(int); // for square
float area3(float); // for circle

float perimeter1(int, int, int); // for triangle
int perimeter2(int); // for square
float perimeter3(float); // for circle

Step 2: Create a C programming file (.c) defining those functions.

#include<stdio.h>
#include<math.h>
#define PI 3.1416

float perimeter1(int a, int b, int c) // for triangle
{
    return a+b+c;
}

int perimeter2(int a) // for square
{
    return 4*a;
}

float perimeter3(float r) // for circle
{
    return 2*PI*r;
}

float area1(int a, int b, int c) // for triangle
{
    float s = perimeter1(a, b, c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}

int area2(int a) // for square
{
    return pow(a,2);
}

float area3(float r) // for circle
{
    return PI*r*r;
}

Step 3: Write your C program (.c) including the areaperi.h file as #include "areaperi.h".

Standard AM, FM and PM Equations

Standard Mathematical Equations for AM, FM and PM

1. Amplitude Modulation (AM)

  • : Carrier amplitude, : Carrier frequency
  • :  Message signal amplitude, : Message signal frequency
  • : Modulation index
  • : Carrier Power, : Sideband Power, : Total Power
  • : Efficiency
  • : Bandwidth of the AM signal

Amplitude Modulation

Q. The equation of amplitude modulated wave is given by s(t) = 40[1+0.8cos(2π ×10^3t)]cos(4π×10^5t). Find the carrier power, the total sideband power and bandwidth of the signal. The value of resistor given is 30Ω.
Solution: Here,

We are given:

s(t)=40[1+0.8cos(2π103t)]cos(4π105t)

And:

  • Load resistance R=30ΩR = 30\, \Omega

Step 1: Identify the standard AM form

Standard AM wave:

s(t)=Ac[1+μcos(2πfmt)]cos(2πfct)

From the given equation:

  • Ac=40A_c = 40

  • μ=0.8

  • fm=103=1kHz

  • fc=4π1052π=2×105=200kHz

a) Carrier Power PcP_c

Pc=Ac22R=402230=160060=26.67W​