ppsc ct-4

 assignment -4 : Download now

Set - III 

1. Write a c program to read,add,and subtract two complex numbers using strutures 


#include <stdio.h>
typedef struct complex {
    float real;
    float imag;
} complex;

complex add(complex n1, complex n2);

int main() {
    complex n1, n2, result;

    printf("For 1st complex number \n");
    printf("Enter the real and imaginary parts: ");
    scanf("%f %f", &n1.real, &n1.imag);
    printf("\nFor 2nd complex number \n");
    printf("Enter the real and imaginary parts: ");
    scanf("%f %f", &n2.real, &n2.imag);

    result = add(n1, n2);

    printf("Sum = %.1f + %.1fi", result.real, result.imag);
    return 0;
}

complex add(complex n1, complex n2) {
    complex temp;
    temp.real = n1.real + n2.real;
    temp.imag = n1.imag + n2.imag;
    return (temp);
}

Output: 


For 1st complex number
Enter the real and imaginary parts: 2.1
-2.3

For 2nd complex number
Enter the real and imaginary parts: 5.6
23.2
Sum = 7.7 + 20.9i

2. Write a c program that coverts lower case to upper case string 

#include <stdio.h>
#include <string.h>
int main() {
   char s[100];
   int i;
   printf("\nEnter a string : ");
   gets(s);
   for (i = 0; s[i]!='\0'; i++) {
      if(s[i] >= 'a' && s[i] <= 'z') {
         s[i] = s[i] -32;
      }
   }
   printf("\nString in Upper Case = %s", s);
   return 0;
}

Set 1 2nd


#include <stdio.h>

 

void main()

{

    char string[50];

    int i, length = 0;

 

    printf("Enter a string \n");

    gets(string);

    /*  keep going through each character of the string till its end */

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

    {

        length++;

    }

    printf("The length of a string is the number of characters in it \n");

    printf("So, the length of %s = %d\n", string, length);

}

Set 2 

1. #include <stdio.h>

struct student {

    char firstName[50];

    int roll;

    float marks;

} s[5];


int main() {

    int i;

    printf("Enter information of students:\n");


    // storing information

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

        s[i].roll = i + 1;

        printf("\nFor roll number%d,\n", s[i].roll);

        printf("Enter first name: ");

        scanf("%s", s[i].firstName);

        printf("Enter marks: ");

        scanf("%f", &s[i].marks);

    }

    printf("Displaying Information:\n\n");


    // displaying information

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

        printf("\nRoll number: %d\n", i + 1);

        printf("First name: ");

        puts(s[i].firstName);

        printf("Marks: %.1f", s[i].marks);

        printf("\n");

    }

    return 0;

}

2. #include <stdio.h>  

#include<string.h>  

int main()  

{  

   char str1[20];  // declaration of char array  

   char str2[20];  // declaration of char array  

   int value; // declaration of integer variable  

   printf("Enter the first string : ");  

   scanf("%s",str1);  

   printf("Enter the second string : ");  

   scanf("%s",str2);  

   // comparing both the strings using strcmp() function  

   value=strcmp(str1,str2);  

   if(value==0)  

   printf("strings are same");  

   else  

   printf("strings are not same");  

   return 0;  

}

Set 4 

1. #include <stdio.h>

void main() 

{

   char str[100], sstr[100];

   int pos, l, c = 0;

   

       printf("\n\nExtract a substring from a given string:\n");

       printf("--------------------------------------------\n");  

 

       printf("Input the string : ");

       fgets(str, sizeof str, stdin);

 

   printf("Input the position to start extraction :");

   scanf("%d", &pos);

   

   printf("Input the length of substring :");

   scanf("%d", &l);

 

   while (c < l) 

   {

      sstr[c] = str[pos+c-1];

      c++;

   }

   sstr[c] = '\0';

 

   printf("The substring retrieve from the string is : \" %s\ "\n\n", sstr);

 

}

2.) A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record. 

Defining a structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than or equal to one member. The format of the struct statement is as follows:

   struct [structure name]
   {
       member definition;
       member definition;
       ...
       member definition;
   };

union

A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes. 


Defining a Union: To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows: 

 
    union [union name]
    {
       member definition;
       member definition;
       ...
       member definition;
    };

Similarities between Structure and Union

  1. Both are user-defined data types used to store data of different types as a single unit.
  2. Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field.
  3. Both structures and unions support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types.
  4. A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable as a corresponding parameter.
  5. ‘.’ operator is used for accessing members.



Previous Post Next Post