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

Programming

Fundamentals
Assignment 3

Ateeb Tahir
23/CS/132
CSE-2
Structure Program 1
Objective
To understand the basics of C syntax in tandem with structure data type
Problem Statement: Write a C program that uses structures to do
operations on complex numbers.
Code
#include <stdio.h>

struct compnum{
float real;
float img;
};
void cadd(struct compnum a,struct compnum b){
struct compnum sum;
sum.real=a.real +b.real;
sum.img=a.img +b.img;
printf("The \nsum of the imaginary numbers is %f +
i(%f)\n",sum.real,sum.img);
}
void csub(struct compnum a,struct compnum b){
struct compnum sub;
sub.real=a.real +-b.real;
sub.img=a.img -b.img;
printf("The difference between the imaginary numbers is %f
+i(%f)\n",sub.real,sub.img);
}
void cmul(struct compnum a,struct compnum b){
struct compnum mul;
mul.real=a.real*b.real -a.img*b.img;
mul.img= a.real*b.img +a.img *b.real;
printf("The multiplication of the imaginary numbers is %f
+i(%f)\n",mul.real,mul.img);
}
void cdiv(struct compnum a,struct compnum b){
struct compnum div;
div.real=(a.real*b.real +a.img*b.img)/(b.real*b.real +b.img*b.img);
div.img=(a.real*(-b.img) +a.img *b.real)/(b.real*b.real +b.img*b.img);
printf("The division of the imaginary numbers is %f
+i(%f)\n",div.real,div.img);
}

int main(){
struct compnum num1,num2;
printf("Enter real and imaginary part of Num1\n");
scanf("%f %f",&num1.real,&num1.img);
printf("Enter real and imaginary part of Num2\n");
scanf("%f %f",&num2.real,&num2.img);

cadd(num1,num2);
csub(num1,num2);
cmul(num1,num2);
cdiv(num1,num2);
return 0;

Output
Structure Program 1
Objective
To understand the basics of C syntax in tandem with structure data type
Problem Statement: Write a C program that uses structures to do
operations on complex numbers.
Code
#include <stdio.h>
#include <math.h>

struct coordinate{
float x;
float y;
};

struct coordinate midpoint(struct coordinate a,struct coordinate b){

struct coordinate mid;


mid.x=0.5*(a.x+b.x);
mid.y=0.5*(a.y+b.y);
return mid;
}

float distance(struct coordinate a,struct coordinate b){

float dist;
dist=sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
return dist;

float area(struct coordinate a,struct coordinate b,struct coordinate c){


float area;
area=0.5*(a.x*(b.y-c.y)-b.x*(c.y-a.y)+c.x*(a.y-b.y));
return area;
}

int main(){
struct coordinate c1,c2,c3,m1,m2,m3;
printf("Enter coordinates 1:\n");
scanf("%f %f",&c1.x,&c1.y);
printf("Enter coordinates 2:\n");
scanf("%f %f",&c2.x,&c2.y);
printf("Enter coordinates 3:\n");
scanf("%f %f",&c3.x,&c3.y);
m1=midpoint(c1,c2);
m2=midpoint(c2,c3);
m3=midpoint(c1,c3);
printf("Distance between c1 and c2 is %f ,between c2 and c3 is %f and
between c1 and c3 is %f\n",distance(c1,c2),distance(c2,c3),distance(c1,c3));
printf("Midpoint between c1 and c2 is (%f,%f) ,between c2 and c3 is (%f,%f)
and between c1 and c3 is (%f,%f)\n",m1.x,m1.y,m2.x,m2.y,m3.x,m3.y);
printf("Area of triangle formed by these is %f",area(c1,c2,c3));
return 0;

Output

You might also like