You are on page 1of 81

1.

Write a program to read x, y coordinates of 3 points and then calculate the area of a
triangle formed by them and print the coordinates of the three points and the area of the
triangle. What will be the output from your program if the three given points are in a
straight line?

Flow Chart:

Computer Programming and Numerical Methods 1


Algorithm:

Step – 1 : Start

Step – 2 : Read the values of the three coordinates

Step – 3 : Print the values of first coordinate

Step – 4 : Print the values of second coordinate

Step – 5 : Print the values of third coordinate

Step – 6 : Calculate Area = 0.5 * fabs (x1 * (y2 – y3)+ x2 * (y3 – y2) +x3 * (y1 – y2))

Step – 7 : Print the area

Step – 8 : If area = 0.

Print the points are collinear otherwise points are not collinear

Step – 9 : Stop

Computer Programming and Numerical Methods 2


Program:

#include<stdio.h>
#include<conio.h>
#include<math.h>

main()
{
int x1, y1, x2, y2, x3, y3;
float area;
clrscr();
printf("Enter the Coordinate \n");
printf("Enter 1st coordinate : \n");
scanf("%d,%d",&x1,&y1);
printf("Enter 2nd coordinate : \n");
scanf("%d,%d",&x2,&y2);
printf("Enter 3rd coordinate : \n");
scanf("%d,%d",&x3,&y3);
printf("The 1st coordinate : %d, %d\n",x1,y1);
printf("The 2nd coordinate : %d, %d\n",x2,y2);
printf("The 3rd coordinate : %d, %d\n",x3,y3);
area = 0.5 * fabs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 -
y2));
printf("area = %lf \n",area);
if(area == 0)
{
printf("The points are Collinear \n");
}
else
{
printf("The points are not Collinear \n");
}
getch();
return 0;
}

Computer Programming and Numerical Methods 3


Output:

Run 1:

Enter the Coordinate


Enter 1st coordinate :
1,2
Enter 2nd coordinate :
3,4
Enter 3rd coordinate :
5,6
The 1st coordinate : 1, 2
The 2nd coordinate : 3, 4
The 3rd coordinate : 5, 6
area = 0.000000
The points are Collinear

Run 2:

Enter the Coordinate


Enter 1st coordinate :
4,7
Enter 2nd coordinate :
9,3
Enter 3rd coordinate :
2,6
The 1st coordinate : 4, 7
The 2nd coordinate : 9, 3
The 3rd coordinate : 2, 6
area = 6.500000
The points are not collinear

Computer Programming and Numerical Methods 4


2. Write a program, which generates 100 random integers in the range of 1 to 100. Store
them in an array and then print the arrays. Write 3 versions of the program using
different loop constructs. (e.g. for, while, and do while)

Flow Chart:

Computer Programming and Numerical Methods 5


Algorithm: (for loop)

Step – 1 : Start

Step – 2 : Read array A[100]

Step – 3 : Declare time_t * timer and srand(time(timer))

Step – 4 : Display random numbers from 1 to 100

Step – 5 : Initialize i = 1 and using conditions i <= 100 and i++

Step – 6 : Calculate A[i] = 1 + rand() % 100

Step – 7 : Print A[i]

Step – 8 : Calculate sum = sum + [i] where we initialize sum = 0

Step – 9 : Display average of random numbers is sum / 100.0

Step – 10 : Stop

Computer Programming and Numerical Methods 6


Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
main()
{
int A[100], i, sum = 0;
float avg;
time_t *timer;
clrscr();
srand(time(timer));
printf("The random numbers from 1 to 100 are :\n");
for(i = 1; i <= 100; i++)
{
A[i] = 1 + rand() % 100;
printf("%d \t",A[i]);
sum += A[i];
avg = sum / 100.0;
}
printf("\nAverage of the random numbers = %.2f\n",avg);
getch();
return 0;
}

Output:
The random numbers from 1 to 100 are :
54 11 53 81 53 69 19 79 82 42
58 69 82 90 29 17 33 29 43 16
28 30 96 19 96 65 78 5 23 2
47 12 72 67 73 88 29 15 63 45
48 71 35 68 82 8 45 1 23 15
47 48 25 50 73 57 89 95 6 55
71 41 67 15 77 96 7 34 93 57
3 97 22 52 64 53 85 42 7 29
80 30 25 50 68 29 50 29 82 76
42 7 50 6 5 78 100 3 18 54

Average of the random numbers = 47.97

Computer Programming and Numerical Methods 7


Flow Chart:

Computer Programming and Numerical Methods 8


Algorithm: (while loop)

Step – 1 : Start

Step – 2 : Read array A[100]

Step – 3 : Declare time_t * timer and srand(time(timer))

Step – 4 : Display random numbers from 1 to 100

Step – 5 : Initialize i = 1 and using conditions i <= 100 and i++

Step – 6 : Check the condition if (i <= 100)

Step – 7 : Calculate A[i] = 1 + rand() % 100

Step – 8 : Print A[i]

Step – 9 : Calculate sum = sum + [i] where we initialize sum = 0

Step – 10 : Display the average of random numbers is sum / 100.0

Step – 11 : Otherwise go to Step–12

Step – 12 : Stop

Computer Programming and Numerical Methods 9


Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
main()
{
int A[100], i = 1, sum = 0;
float avg;
time_t *timer;
clrscr();
srand(time(timer));
printf("The random numbers from 1 to 100 are :\n");
while(i <= 100)
{
A[i] = 1 + rand() % 100;
printf("%d \t",A[i]);
sum += A[i];
i++;
}
avg = sum / 100.0;
printf("\nAverage of the random numbers = %.2f\n",avg);
getch();
return 0;
}

Output:
The random numbers from 1 to 100 are :
28 78 69 42 43 81 21 67 62 35
81 6 58 14 31 68 52 24 15 66
51 49 49 89 49 61 40 14 28 27
37 43 3 86 84 99 42 58 5 91
38 18 75 53 45 52 57 25 58 25
16 58 11 91 28 51 40 32 21 47
15 17 49 1 35 83 48 8 5 24
16 29 61 8 54 42 82 73 9 54
17 12 97 22 99 82 82 91 69 36
9 17 55 94 18 4 32 90 88 96

Average of the random numbers = 46.09

Computer Programming and Numerical Methods 10


Flow Chart:

Computer Programming and Numerical Methods 11


Algorithm: (do while loop)

Step – 1 : Start

Step – 2 : Read array A[100]

Step – 3 : Declare time_t * timer and srand(time(timer))

Step – 4 : Display random numbers from 1 to 100

Step – 5 : Initialize i = 1 and using conditions i <= 100 and i++

Step – 6 : Check the condition if (i <= 100)

Step – 7 : Calculate A[i] = 1 + rand() % 100

Step – 8 : Print A[i]

Step – 9 : Calculate sum = sum + [i] where we initialize sum = 0

Step – 10 : Display the average of random numbers is sum / 100.0

Step – 11 : Otherwise go to Step–12

Step – 12 : Stop

Computer Programming and Numerical Methods 12


Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
main()
{
int A[100], i = 1, sum = 0;
float avg;
time_t *timer;
clrscr();
srand(time(timer));
printf("The random numbers from 1 to 100 are :\n");
do
{
A[i] = 1 + rand() % 100;
printf("%d \t",A[i]);
sum += A[i];
i++;
}while(i <= 100);
avg = sum / 100.0;
printf("\nAverage of the random numbers = %.2f\n",avg);
getch();
return 0;
}

Output:
The random numbers from 1 to 100 are :
97 66 49 54 99 39 25 13 51 98
99 39 87 27 15 67 53 27 97 36
89 13 69 10 18 5 88 85 8 1
56 44 86 78 75 64 18 99 81 37
9 72 33 87 20 45 78 48 10 47
2 9 9 23 34 3 95 46 98 44
58 44 87 70 66 73 96 59 76 88
65 61 100 65 98 27 55 74 15 64
16 97 16 72 43 97 59 81 42 14
80 75 87 54 21 70 51 21 65 91

Average of the random numbers = 54.66

Computer Programming and Numerical Methods 13


3. Write a set of string manipulation functions e.g. for getting a substring from a given
position, Copying one string to another. Reversing a string, adding one string to another.

Flow Chart:

Computer Programming and Numerical Methods 14


Computer Programming and Numerical Methods 15
Computer Programming and Numerical Methods 16
Computer Programming and Numerical Methods 17
Algorithm:

Step – 1 : Start

Step – 2 : Declare arrays s[15], d[15] variables choice, pos, len, l

Step – 3 : Print 1. Copy 2. Substring 3. Reverse 4. Concatenation 5. Length 6. Exit

Step – 4 : If 1 Print enter your choice and execute from Step to Step otherwise go to
Stop

Step – 5 : If choice is 1. Print enter the source string and go to next Step otherwise go to
Step–13

Step – 6 : Declare variable i

Step – 7 : Initialize i to zero

Step – 8 : If s[i] not equal to NULL character is true then go to next Step otherwise go to
Step–11

Step – 9 : Copy s[i] into d[i]

Step – 10 : Increment i by 1 and go to Step–8

Step – 11 : Assign d[i] equal to NULL character

Step – 12 : Print the copied string ‘d’

Step – 13 : If choice is 2. Print enter the source string position, length and go to next Step
otherwise go to Step–21

Step – 14 : Declare variable ‘i’

Step – 15 : Initialize i to zero

Step – 16 : If i less than Len go to next Step otherwise go to Step–19

Step – 17 : Calculate d[i] = s[pos + i – 1]

Step – 18 : Increment i by 1 and go to Step–16

Step – 19 : Assign d[i] equal to NULL character

Step – 20 : Print substring d

Step – 21 : If choice is 3. Print enter the source string and declare variable i, j, temp and
go to next Step otherwise go to Step–25. Initialize i to zero and j to strlen(s)–1

Step – 22 : If i < j is true then perform temp = s[i], s[i] = s[j] and s[j] = temp and go to next

Computer Programming and Numerical Methods 18


Step otherwise go to Step–25

Step – 23 : Increment i by 1, decrement j by 1 and go to Step–22

Step – 24 : Print reversed string s

Step – 25 : If choice is 4. Print enter the source string and destination string otherwise go
to Step–24

Step – 26 : Declare variables len, i

Step – 27 : Assign len = strlen(s)

Step – 28 : Initialize i to zero

Step – 29 : If s[i] not equal to NULL characters is true go to next Step otherwise go to
Step–33

Step – 30 : Calculate or assign d[len + i] = s[i]

Step – 31 : Assign d[len + i] equal to NULL character

Step – 32 : Increment i by 1 and go to Step–29

Step – 33 : Print the concatenated string ‘d’

Step – 34 : If choice is 5. Print enter the source string declare variable i and go to next
Step. Otherwise go to Stop

Step – 35 : If s[i] not equal to NULL character, increment length by 1 otherwise go to


Step–38

Step – 36 : Return length to main

Step – 37 : Print length of the string ‘a’

Step – 38 : If choice is 6. Exit from menu list

Step – 39 : Stop

Computer Programming and Numerical Methods 19


Program:

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

void string_copy(char d[],char s[])


{
int i;
for(i = 0; s[i]!='\0'; i++)
d[i] = s[i];
d[i] = '\0';
}

void substring(char d[], char s[], int pos, int len)


{
int i;
for(i = 0; i < len; i++)
d[i] = s[pos - 1 + i];
d[i] = '\0';
}

void string_concat(char d[],char s[])


{
int i, len;
len = string_length(d);
for(i = 0; s[i] != '\0'; i++)
{
d[len+i] = s[i];
}
d[len+i] = '\0';
}

int string_length(char s[])


{
int i, length = 0;
for(i = 0; s[i] !='\0'; i++)
length++;
return(length);
}

void string_reverse(char s[])


{
int i, j;
char temp;

Computer Programming and Numerical Methods 20


for(i = 0, j = string_length(s)-1; i < j; i++, j--)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}

main()
{
int len, pos, choice, a;
char s[50], d[50];
clrscr();
printf("\n Welcome to storing functions \n");
printf("\n 1. Copy \n 2. Substring \n 3. Concatenation \n
4. Reverse \n 5. Length \n 6. Exit \n");

do
{
printf("Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the string :");
scanf("%s",s);
string_copy(d,s);
printf("\n The copied string is %s \n",d);
break;
case 2:
printf("\n Enter the string :");
scanf("%s",s);
printf("\n Enter the starting position and length of string
\n");
scanf("%d %d",&pos,&len);
substring(d,s,pos,len);
printf("\n The substring is %s \n",d);
break;
case 3:
printf("\n Enter the string :");
scanf("%s %s",d,s);
string_concat(d,s);
printf("\n The string is %s \n",d);
break;
case 4:
printf("\n Enter the string :");
scanf("%s",s);

Computer Programming and Numerical Methods 21


string_reverse(s);
printf("\n The reverse of the string is %s \n",s);
break;
case 5:
printf("\n Enter the string :");
scanf("%s",s);
a = string_length(s);
printf("\n The length of the string is %d \n",a);
break;
case 6: exit(0);
default:printf("\n Invalid choice \n");
}
}while(choice != 7);
return 0;
}

Computer Programming and Numerical Methods 22


Output:
Welcome to storing functions

1. Copy
2. Substring
3. Concatenation
4. Reverse
5. Length
6. Exit
Enter your choice :1

Enter the string :Engineering


The copied string is Engineering

Enter your choice :2

Enter the string :Engineering


Enter the starting position and length of string
55
The substring is neeri

Enter your choice :3

Enter the string :Chemical Engineering


The string is ChemicalEngineering

Enter your choice :4

Enter the string :Engineering


The reverse of the string is gnireenignE

Enter your choice :5

Enter the string :Engineering


The length of the string is 11

Enter your choice :6

Computer Programming and Numerical Methods 23


4. Write a program, which determines the largest and the smallest number that can be
stored in different data types like short, int, long, float and double. What happens when
you add 1 to the largest possible integer number that can be stored?

Flow Chart:

Computer Programming and Numerical Methods 24


Algorithm:

Step – 1 : Start

Step – 2 : Initialize a, b

Step – 3 : Print maximum limit of integer

Step – 4 : Print minimum limit of integer

Step – 5 : Print maximum limit of short integer

Step – 6 : Print minimum limit of short integer

Step – 7 : Print maximum limit of long integer

Step – 8 : Print minimum limit of long integer

Step – 9 : Print maximum limit of unsigned integer

Step – 10 : Print maximum limit of unsigned short integer

Step – 11 : Print maximum limit of unsigned long integer

Step – 12 : Print maximum limit of float

Step – 13 : Print minimum limit of float

Step – 14 : Print maximum limit of double

Step – 15 : Print minimum limit of double

Step – 16 : Print maximum limit of long double

Step – 17 : Print minimum limit of long double

Step – 18 : Print maximum limit of character

Step – 19 : Print minimum limit of character

Step – 20 : Print maximum limit of signed character

Step – 21 : Print minimum limit of signed character

Step – 22 : Print maximum limit of unsigned character

Step – 23 : Print a = INT_MAX

Step – 24 : Print a + 1

Step – 25 : Print b = INT_MIN

Computer Programming and Numerical Methods 25


Step – 26 : Print b + 1

Step – 27 : Stop

Computer Programming and Numerical Methods 26


Program:

#include<stdio.h>
#include<conio.h>
#include<float.h>
#include<limits.h>

main()
{
int a, b;
clrscr();
printf("Maximum limit of integer : %d \n",INT_MAX);
printf("Minimum limit of integer : %d \n",INT_MIN);
printf("Maximum limit of short integer : %d \n",SHRT_MAX);
printf("Minimum limit of short integer : %d \n",SHRT_MIN);
printf("Maximum limit of long integer : %d \n",LONG_MAX);
printf("Minimum limit of long integer : %d \n",LONG_MIN);
printf("Maximum limit of unsigned integer : %u \n",UINT_MAX);
printf("Maximum limit of unsigned short integer : %u
\n",USHRT_MAX);
printf("Maximum limit of unsigned long integer : %u
\n",ULONG_MAX);
printf("Maximum limit of float : %E \n",FLT_MAX);
printf("Minimum limit of float : %E \n",FLT_MIN);
printf("Maximum limit of double : %le \n",DBL_MAX);
printf("Minimum limit of double : %le \n",DBL_MIN);
printf("Maximum limit of long double : %Le \n",LDBL_MAX);
printf("Minimum limit of long double : %Le \n",LDBL_MIN);
printf("Maximum limit of double : %le \n",DBL_MAX);
printf("Minimum limit of double : %le \n",DBL_MIN);
printf("Maximum limit of character : %d \n",CHAR_MAX);
printf("Minimum limit of character : %d \n",CHAR_MIN);
printf("Maximum limit of signed character : %d \n",SCHAR_MAX);
printf("Minimum limit of signed character : %d \n",SCHAR_MIN);
printf("Maximum limit of unsigned character : %d \n",UCHAR_MAX);
a = INT_MAX;
printf("If we add 1 to maximum possible integer then they
obtained value is %d \n", a + 1);
b = INT_MIN;
printf("If we add 1 to minimum possible integer then they
obtained value is %d \n", b + 1);
getch();
return 0;
}

Computer Programming and Numerical Methods 27


Output:

Maximum limit of integer : 32767


Minimum limit of integer : -32768
Maximum limit of short integer : 32767
Minimum limit of short integer : -32768
Maximum limit of long integer : -1
Minimum limit of long integer : 0
Maximum limit of unsigned integer : 65535
Maximum limit of unsigned short integer : 65535
Maximum limit of unsigned long integer : 65535
Maximum limit of float : 3.402823E+38
Minimum limit of float : 1.175494E-38
Maximum limit of double : 1.797693e+308
Minimum limit of double : 2.225074e-308
Maximum limit of long double : 1.189731e+4932
Minimum limit of long double : 1.000000e-4932
Maximum limit of double : 1.797693e+308
Minimum limit of double : 2.225074e-308
Maximum limit of character : 127
Minimum limit of character : -128
Maximum limit of signed character : 127
Minimum limit of signed character : -128
Maximum limit of unsigned character : 255
If we add 1 to maximum possible integer then obtained value is -32768
If we add 1 to minimum possible integer then obtained value is -32767

Computer Programming and Numerical Methods 28


5. Write a program, which generates 100 random real numbers in the range of 10.0 to 20.0
and sort them in descending order.

Flow Chart:

Computer Programming and Numerical Methods 29


Algorithm:

Step – 1 : Start

Step – 2 : Declare array a[150]

Step – 3 : Initialize i = 1 and using conditions i <= 100 and i++

Step – 4 : Calculate a[i] = 10.0 + random(1000) / 100.0

Step – 5 : Generate 100 real numbers between 10.0 and 20.0 and store them in the
array a[100]

Step – 6 : Print the array a[150]

Step – 7 : Sort the elements in a descending order and stored into array a[150]

Step – 8 : Print the stored array a[150]

Step – 9 : Stop

Computer Programming and Numerical Methods 30


Program:

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

main()
{
int i, j;
float a[150], temp;
clrscr();
for(i = 0; i < 100; i++)
{
a[i] = 10.0 + random(1000) / 100.0;
}
printf("The generated 100 random real numbers : \n");
for(i = 0; i < 100; i++)
{
printf("%.2f \t",a[i]);
}
/* Sorting */
for(i = 0; i < 100; i++)
{
for(j = 0; j < 100; j++)
{
if(a[j] < a[j+1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("The generated 100 random real numbers in descending
order : \n");
for(i = 0; i < 100; i++)
{
printf("%.2f \t",a[i]);
}
getch();
return 0;
}

Computer Programming and Numerical Methods 31


Output:

The generated 100 random real numbers :

10.10 10.03 13.35 10.33 13.55 12.17 15.36 11.95 17.00 19.49
12.74 14.44 11.08 16.98 15.64 10.41 11.65 18.15 16.85 17.64
18.27 19.59 12.19 14.26 19.52 18.39 19.23 18.10 14.51 16.04
16.61 15.99 15.49 17.20 11.13 14.06 11.21 16.71 14.74 14.91
15.64 13.44 18.68 12.64 11.79 14.23 16.94 11.63 15.38 16.45
16.23 10.03 17.87 12.68 14.61 13.86 13.76 15.81 16.03 12.79
11.70 18.05 12.94 13.33 14.08 12.40 14.13 10.54 14.94 19.83
10.01 14.09 10.69 10.73 12.54 19.74 13.55 14.04 11.97 11.97
12.11 12.49 17.58 18.89 19.05 17.35 14.61 15.31 10.35 11.30
14.58 14.83 15.96 12.53 11.14 17.01 16.49 18.86 18.75 10.42

The generated 100 random real numbers in descending order :

19.83 19.74 19.59 19.52 19.49 19.23 19.05 18.89 18.86 18.75
18.68 18.39 18.27 18.15 18.10 18.05 17.87 17.64 17.58 17.35
17.20 17.01 17.00 16.98 16.94 16.85 16.71 16.61 16.49 16.45
16.23 16.04 16.03 15.99 15.96 15.81 15.64 15.64 15.49 15.38
15.36 15.31 14.94 14.91 14.83 14.74 14.61 14.61 14.58 14.51
14.44 14.26 14.23 14.13 14.09 14.08 14.06 14.04 13.86 13.76
13.55 13.55 13.44 13.35 13.33 12.94 12.79 12.74 12.68 12.64
12.54 12.53 12.49 12.40 12.19 12.17 12.11 11.97 11.97 11.95
11.79 11.70 11.65 11.63 11.30 11.21 11.14 11.13 11.08 10.73
10.69 10.54 10.42 10.41 10.35 10.33 10.10 10.03 10.03 10.01

Computer Programming and Numerical Methods 32


6. Write a function for transposing a square matrix in place (in place means that you are not
allowed to have full temporary matrix)

Flow Chart:

Computer Programming and Numerical Methods 33


Computer Programming and Numerical Methods 34
Algorithm:

Step – 1 : Start

Step – 2 : Declare the two dimensional array a[10][10]. Variables I, j, m, n where m, n


are no. of rows and columns

Step – 3 : If m = n enter elements into an array and generate from Step–4 to Step–13.
Otherwise print transpose of given matrix is not possible and go to Step–15

Step – 4 : Print the matrix as follows

Step – 5 : Initialize i to zero

Step – 6 : Check whether is less than n. If true then initialize j to zero. Otherwise go to
Step–9

Step – 7 : Check whether is less than n. If true go to Step–8 and if false Step–6 by
incrementing i i.e., i+1.

Step – 8 : Print a[i][i] matrix, increment j by 1 by checking condition

Step – 9 : Initialize i to zero

Step – 10 : Check whether i < n. If true initialize j to zero and go to Step–11. Otherwise
increment i

Step – 11 : Check whether j is less than n. If true check whether i is less than j. If i < j is
true swap a[i][j] and a[j][i] and if i < j s false, increment j by 1 and repeat same
Step j < n and if j < n is false go to Step–10 and increment i by 1

Step – 12 : Initialize I to zero

Step – 13 : Check whether i < n. If true initialize j to zero and got Step–14. Otherwise go
to Step–15

Step – 14 : Check whether j < n. If true print the transposed matrix a[i][j] by incrementing
j by 1 and if false increment i by 1 and go to Step–13

Step – 15 : Stop

Computer Programming and Numerical Methods 35


Program:

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

main()
{
int a[10][10], i, j, m, n;
clrscr();
printf("Enter the order of the matrix: \n");
scanf("%d %d",&m, &n);
if(m == n)
{
printf("Type in matrix form: \n");
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The given matrix is as follows \n");
print_matrix(a, n);
printf("The transposed matrix is as follows \n");
transpose_matrix(a,n);
print_matrix(a, n);
}
else
{
printf("For given values, transpose of matrix is not possible
:");
}
getch();
return 0;
}

print_matrix(int a[10][10], int n)


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

Computer Programming and Numerical Methods 36


printf("\n");
}
}

transpose_matrix(int a[10][10], int n)


{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(i < j)
{
a[i][j] = a[i][j] + a[j][i];
a[j][i] = a[i][j] - a[j][i];
a[i][j] = a[i][j] - a[j][i];
}
}
}
}

Computer Programming and Numerical Methods 37


Output:

Run 1:

Enter the order of the matrix:


22
Type in matrix form:
1
2
3
4
The given matrix is as follows
1 2
3 4
The transposed matrix is as follows
1 3
2 4

Run 2:

Enter the order of the matrix:


3
3
Type in matrix form:
234567894
The given matrix is as follows
2 3 4
5 6 7
8 9 4
The transposed matrix is as follows
2 5 8
3 6 9
4 7 4

Computer Programming and Numerical Methods 38


7. First use an editor to create a file with some integer numbers. Now write a program,
which reads these numbers and determines their mean and standard deviation.

Flow Chart:

Computer Programming and Numerical Methods 39


Algorithm:

Step – 1 : Start

Step – 2 : Initialize sum = 0, sum_sq = 0, count = 0, mean = 0 and sd = 0

Step – 3 : Open a file ‘mean.dat’ in write mode

Step – 4 : Check the condition if(fp==NULL)

Step – 5 : Print the integers

Step – 6 : To Stop entry press Ctrl + z

Step – 7 : If end of the input is reached return EOF

Step – 8 : Close the file

Step – 9 : Open the file ‘mean.dat’ in read mode

Step – 10 : Print file to be open if(fp==NULL) condition is true and exit if file ended

Step – 11 : Print numbers in file

Step – 12 : If check the condition while end is not reached

Step – 13 : If true

(i) Read integer numbers from file

(ii) Print the number

(iii) Increment sum by no.

(iv) Increment count by 1.

(v) Increment sum by no. * no.

Step – 14 : Close the file

Step – 15 : Mean = sum / count

Step – 16 : Sd = sqrt((sum_sq / count )–(mean * mean))

Step – 17 : Print mean, standard deviation

Step – 18 : Stop

Computer Programming and Numerical Methods 40


Program:

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

main()
{
FILE *fp;
int n, count = 0, sum = 0, sumsq = 0;
float mean = 0.0, sd = 0.0;
clrscr();
fp = fopen("mean.dat","w");
if(fp==NULL)
{
puts("\n Can't open the file ");
exit(0);
}
printf("\n Enter some numbers. To stop use Ctrl + z :\n");
while(scanf("%d",&n)!=EOF)
fprintf(fp,"%d\t",n);
fclose(fp);
fp = fopen("mean.dat","r");
printf("\n The numbers stored in file : \n");
while(fscanf(fp,"%d",&n)!=EOF)
{
count++;
sum = sum + n;
printf("%d\t",n);
sumsq += n * n;
}
fclose(fp);
mean = sum / count;
printf("\n Mean = %0.2f",mean);
sd = sqrt((sumsq/count)-(mean*mean));
printf("\n standard derivation = %0.2f\n",sd);
getch();
return 0;
}

Computer Programming and Numerical Methods 41


Output:

Enter some numbers to stop use Ctrl + z:


1.2
2.1
2.3
7.8
5.6
3.4
3.1
9.0
^Z
The numbers stored in file :
1.200000 2.100000 2.300000 7.800000 5.600000
3.400000 3.100000 9.000000
Mean = 4.31
Standard derivation = 2.66

Computer Programming and Numerical Methods 42


8. Give two points on the surface of the sphere; Write a program to determine the smallest
arc length between them.

Flow Chart:

Computer Programming and Numerical Methods 43


Algorithm:

Step – 1 : Start

Step – 2 : Read the two points on the surface of the sphere

Step – 3 : Calculate r  x  x   y  y   z  z  


1
2
1
2
1
2

Step – 4 : Calculate d  x  x   y  y   z  z  


1 2
2
1 2
2
1 2
2

Step – 5 : Calculate A = 2 * asin (d / (2 * r))

Step – 6 : Calculate l = (r * A)

Step – 7 : Print arc length

Step – 8 : Stop

Computer Programming and Numerical Methods 44


Program:

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

main()
{
int x, y, z, x1, y1, z1, x2, y2, z2;
double r, l, d, A;
clrscr();
printf("Enter x, y, z, co-ordinates of the centre of the sphere
\n");
scanf("%d%d%d",&x,&y,&z);
printf("Enter x1, y1, z1, co-ordinates \n");
scanf("%d%d%d",&x1,&y1,&z1);
printf("Enter x2, y2, z2, co-ordinates \n");
scanf("%d%d%d",&x2,&y2,&z2);
r=sqrt(pow((x-x1),2)+pow((y-y1),2)+pow((z-z1),2));
d=sqrt(pow((x1-x2),2)+pow((y1-y2),2)+pow((z1-z2),2));
A = 2 * asin(d / (2 * r));
l=(r * A);
printf("\nThe smallest arc length between the given points on
the sphere is %f limits",l);
getch();
}

Output:

Enter x, y, z, co-ordinates of the centre of the sphere


123
Enter x1, y1, z1, co-ordinates
323
Enter x2, y2, z2, co-ordinates
103

The smallest arc length between the given points on the sphere is 3.141593 limits

Computer Programming and Numerical Methods 45


9. Implement bisection method to find the square root of a given number to a given
accuracy.

Flow Chart:

Computer Programming and Numerical Methods 46


Algorithm:

Step – 1 : Start

Step – 2 : Read a, b, epsilon, xmid

Step – 3 : Enter the no. for which square root has to be found

Step – 4 : Enter the initial guess values and accuracy

Step – 5 : If(f(a) * f(b) > 0)

Step – 6 : Print unsuitable initial values

Step – 7 : Exit

Step – 8 : Xmid = (a + b) / 2

Step – 9 : While(fabs(xmid - b) / xmid >= epsilon)

Step – 10 : If f(xmid == 0)

Step – 11 : Break

Step – 12 : If(f(a) * f(xmid) < 0)

Step – 13 : B = xmid

Step – 14 : Else a = xmid

Step – 15 : Xmid = a + b / 2

Step – 16 : Go to Step–10

Step – 17 : Print the square root is xmid

Step – 18 : Stop

Computer Programming and Numerical Methods 47


Program:

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

float n;
float f(float x)
{
return(x * x - n);
}

main()
{
float a, b, epsilon, xmid;
clrscr();
printf("\nEnter the no. of which square root has to be found :
\n");
scanf("%f",&n);
printf("\nEnter the initial guess values and accuracy: \n");
scanf("%f,%f,%f",&a, &b, &epsilon);
if(f(a) * f(b) > 0)
{
printf("\n Unsuitable initial values \n");
exit(0);
}

xmid = (a + b) / 2;
while(fabs(xmid - b) / xmid >= epsilon)
{
if(f(xmid) == 0)
break;
if(f(a) * f(xmid) < 0)
b = xmid;
else
a = xmid;
xmid = (a + b) / 2;
}
printf("\nThe Square root = %2f \n",xmid);
getch();
return 0;
}

Computer Programming and Numerical Methods 48


Output:

Run 1:

Enter the no. of which square root has to be found :


25

Enter the initial guess values and accuracy:


4,6,0.0001

The Square root = 5.000000

Run 2:

Enter the no. of which square root has to be found :


49

Enter the initial guess values and accuracy:


4,6,0.0001

Unsuitable initial values

Computer Programming and Numerical Methods 49


10. Implement Newton Raphson method to determine a root of polynomial equation.

Flow Chart:

Computer Programming and Numerical Methods 50


Algorithm:

Step – 1 : Start

Step – 2 : Declare x, epsilon, slope, xnew, rerror

Step – 3 : Print enter initial approximations to root

Step – 4 : Enter accuracy and limit on allowed slope

Step – 5 : If(f’(x) < slope) if true go to Step–6 otherwise go to Step–11

Step – 6 : Print slope is too small

Step – 7 : Calculate xnew = x - (f(x) / f1(x))

Step – 8 : Calculate rerror = fabs((xnew - x) / xnew)

Step – 9 : Calculate x = xnew

Step – 10 : Check whether rerror >= epsilon

Step – 11 : Print the root as xnew

Step – 12 : Stop

Step – 1 : Call function f(float x)

Step – 2 : return(x * x * x - 2 * x - 5)

Step – 3 : Call function f1(float x)

Step – 4 : return(3 * x * x - 2)

Computer Programming and Numerical Methods 51


Program:

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

float f(float x)
{
return(x * x * x - 2 * x - 5);
}
float f1(float x)
{
return(3 * x * x - 2);
}

main()
{
float x, epsilon, slope, xnew, rerror;
clrscr();
printf("\nEnter Initial approximation to root : \n");
scanf("%f",&x);
printf("\nEnter accuracy and limit on allowed slope : \n");
scanf("%f,%f",&epsilon, &slope);
do
{
if(f1(x) < slope)
{
printf("slope is too small \n");
break;
}
xnew = x - (f(x) / f1(x));
rerror = fabs((xnew - x) / xnew);
x = xnew;
}
while(rerror >= epsilon);
printf("Root = %f ", xnew);
getch();
return 0;
}

Computer Programming and Numerical Methods 52


Output:

Run 1:

Enter Initial approximation to root :


3

Enter accuracy and limit on allowed slope :


0.001,0
Root = 2.094552

Run 2:

Enter Initial approximation to root :


1

Enter accuracy and limit on allowed slope :


0.001,2
slope is too small
Root = 0.000000

Computer Programming and Numerical Methods 53


11. Given a table of x and corresponding f(x) values, write a program which will determine
f(x) value at an intermediate x value using Lagrange’s interpolation.

Flow Chart:

Computer Programming and Numerical Methods 54


Computer Programming and Numerical Methods 55
Algorithm:

Step – 1 : Start

Step – 2 : Declare arrays x[10], f[10], variables, sum, product, y, i, j, n

Step – 3 : Initialize sum to zero

Step – 4 : Print enter value of n

Step – 5 : Print enter x[i] value s

Step – 6 : Initialize i to zero

Step – 7 : Check whether i < n if true go to next Step else go to Step–9

Step – 8 : Read x[i] values, increment i by 1 and go to Step–7

Step – 9 : Print function values

Step – 10 : Initialize i to zero

Step – 11 : Check whether i < n if true go to next Step else go to Step–13

Step – 12 : Read f[i] values, increment i by 1 and go to Step–11

Step – 13 : Print x[i], f[i] values

Step – 14 : Initialize i to zero

Step – 15 : Check whether i < n if true execute Step–16 else go to Step–17

Step – 16 : Print x[i], f[i] values, increment i by 1 and go to Step–15

Step – 17 : Print enter intermediate value

Step – 18 : Initialize i to zero

Step – 19 : Check whether i < n if true execute from Step–19 to Step–25 otherwise go to
Step–26

Step – 20 : Initialize product = 1

Step – 21 : Initialize j to zero

Step – 22 : Check whether j < n if true execute from Step–23 to Step–24 otherwise go to
Step–25

Step – 23 : Check whether i!=j if true go to Step–24 otherwise increment by 1 and go to


Step–22

Computer Programming and Numerical Methods 56


Step – 24 : Calculate product *=((y-x[j])/(x[i] - x[j]))

Step – 25 : Calculate sum += f[i] * product

Step – 26 : Print interpolated values sum

Step – 27 : Stop

Computer Programming and Numerical Methods 57


Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int i, j, n;
float x[10], f[10], sum = 0, product = 1, y;
clrscr();
printf("Enter the value of n \n");
scanf("%d",&n);
printf("Enter %d values of x, f(x) \n",n);
for(i = 0; i < n; i++)
scanf("%f,%f",&x[i],&f[i]);
printf("Enter the intermediate value \n");
scanf("%f",&y);
printf("\n x f(x) \n");
for(i = 0; i < n; i++)
printf("\n %f \t %f\n",x[i],f[i]);
for(i = 0; i < n; i++)
{
product = 1;
for(j = 0; j< n; j++)
{
if(j != i)
product *=((y-x[j])/(x[i] - x[j]));
}
sum += f[i] * product;
}
printf("y = %f \t sum = %f \n",y,sum);
getch();
return 0;
}

Computer Programming and Numerical Methods 58


Output:

Enter the value of n


3
Enter 3 values of x, f(x)
5.0,7.0
6.0,6.0
7.0,5.0
Enter the intermediate value
6.5

x f(x)

5.000000 7.000000
6.000000 6.000000
7.000000 5.000000

y = 6.500000 sum = 5.500000

Computer Programming and Numerical Methods 59


12. Write a function which will invert a matrix

Flow Chart:

Computer Programming and Numerical Methods 60


Computer Programming and Numerical Methods 61
Computer Programming and Numerical Methods 62
Computer Programming and Numerical Methods 63
Algorithm:

Step – 1 : Start

Step – 2 : Declare array a[10][10], variables i, j, n

Step – 3 : Print enter order of matrix

Step – 4 : Initialize i to zero

Step – 5 : Check whether i < n if true go to next Step and execute from Step–9 otherwise
increment i by 1

Step – 6 : Initialize j to zero

Step – 7 : Check whether j < n if true execute Step–8 and Step–9 otherwise increment i
by 1and go to Step–5

Step – 8 : Read elements into an array

Step – 9 : Increment j by 1 and go to Step–7

Step – 10 : Call function inverse(a, n)

Step – 11 : Declare variables I, j, k array b[10][10]

Step – 12 : Initialize i to zero

Step – 13 : Check whether i < n if true execute from Step–14 to Step–19 otherwise go to
Step–20

Step – 14 : Initialize j to zero

Step – 15 : Check whether j < n if true execute Step–16 and Step–17 otherwise go to
Step–18

Step – 16 : Calculate b[i][j] = 0.0

Step – 17 : Increment j by 1 and go to Step–13

Step – 18 : b[i][i] = 1.0

Step – 19 : Increment i by 1 and go to Step–13

Step – 20 : Initialize k = 0

Computer Programming and Numerical Methods 64


Step – 21 : Check whether k < n if true execute from Step–22 to Step–31 otherwise go to
Step–30

Step – 22 : Initialize i to zero

Step – 23 : Check whether i < n if true executes from Step–24 to Step–31 otherwise go to
Step–21 by increment k by 1

Step – 24 : Check whether i == k if true go to Step–21 otherwise go to Step–25

Step – 25 : Calculate r = a[i][k] / a[k][k]

Step – 26 : Initialize j to zero

Step – 27 : Check whether j is less than if true execute from Step–28 to Step–30
otherwise go to Step–23 by i + 1

Step – 28 : Calculate a[i][j] -=r * a[k][j]

Step – 29 : Calculate a[i][j] -=r * b[k][j]

Step – 30 : Increment j by 1 and go to Step–27

Step – 31 : Initialize i to zero

Step – 32 : Check whether i < n if true execute from Step–33 to Step–36 otherwise go to
Step–37

Step – 33 : Initialize j to zero

Step – 34 : Check whether j < n if true execute Step–35 and Step–36 otherwise go to
Step–32 by incrementing i by 1

Step – 35 : Calculate b[i][j] = a[i][j] / a[i][i]

Step – 36 : Increment j by 1 and go to Step–34

Step – 37 : Initialize i to zero

Step – 38 : Check whether i < n if true execute from Step–39 to Step–42 otherwise return
to main

Step – 39 : Initialize j to zero

Step – 40 : Check whether j < n if true execute Step–40 and Step–41 otherwise increment

Computer Programming and Numerical Methods 65


i by 1 and go to Step - 38

Step – 41 : Print b[i][j]

Step – 42 : Increment j by 1 and go to Step–39

Step – 43 : Stop

Computer Programming and Numerical Methods 66


Program:

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

void inverse(float a[10][10],int n)


{
int i, j, k;
float b[10][10], r;
for(i = 0; i< n; i++)
{
for(j = 0; j < n; j++)
b[i][j] = 0.0;
b[i][i] = 1.0;
}
for(k = 0; k < n; k++)
for(i = 0; i < n; i++)
{
if(i == k)
continue;
r = a[i][k] / a[k][k];
for(j = 0; j < n; j++)
{
a[i][j] -=r * a[k][j];
b[i][j] -= r * b[k][j];
}
}
for(i = 0; i<n; i++)
for(j = 0; j<n; j++)
b[i][j] = b[i][j] / a[i][i];
printf("The inverse matrix is \n");
for(i = 0; i <n; i++)
{
for(j = 0; j < n; j++)
printf("%3f \t",b[i][j]);
printf("\n");
}
}

main()
{
int i, j, k, n;
float a[10][10];
clrscr();
printf("Enter the order of the matrix\n");

Computer Programming and Numerical Methods 67


scanf("%d",&n);
printf("Enter the elements of matrix \n");
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
scanf("%f",&a[i][j]);
inverse(a,n);
getch();
return 0;
}

Output:

Run 1:

Enter the order of the matrix


2
Enter the elements of matrix
1234
The inverse matrix is
-2.000000 1.000000
1.500000 -0.500000

Run 2:

Enter the order of the matrix


3
Enter the elements of matrix
1.0 2.0 0.0 2.0 3.0 0.5 1.0 1.0 1.0

The inverse matrix is

-5.000000 4.000000 -2.000000


3.000000 -2.000000 1.000000
2.000000 -2.000000 2.000000

Computer Programming and Numerical Methods 68


13. Implement Simpson’s rule for numerical integration.

Flow Chart:

Computer Programming and Numerical Methods 69


Algorithm:

Step – 1 : Start

Step – 2 : Declare i, n as integers

Step – 3 : Declare s1 = 0, s2 = 0, a, b, h, x, integral as float variables

Step – 4 : Read a, b

Step – 5 : Enter the number of steps i.e., value of n

Step – 6 : Assign h = (b – a)/n

Step – 7 : Assign x = a

Step – 8 : Initialize i = 1

Step – 9 : If i < n condition is true go to Step–10 else Step–15

Step – 10 : Assign x += h

Step – 11 : If(i % 2 == 0) go to Step–12 else go to Step–13

Step – 12 : Assign s1 += 2 * f(x)

Step – 13 : Assign s2 += 4 * f(x)

Step – 14 : Increment i value and go to Step–9

Step – 15 : Assign integral value i.e, integral = (h/3) * (f(a) + s1 + s2 + f(b))

Step – 16 : Print the values of integral

Step – 17 : Stop

Computer Programming and Numerical Methods 70


Program:

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

float f(float x)
{
return(1/(1 + x));
}

main()
{
int i, n;
float s1 = 0, s2 = 0, a, b, h, x, integral;
clrscr();
printf("Enter a and b limits \n");
scanf("%f,%f",&a,&b);
printf("Enter no. of steps : \n");
scanf("%d",&n);
h = (b - a) / n;
x = a;
for(i = 1; i < n; i++)
{
x += h;
if(i % 2 == 0)
s1 += 2 * f(x);
else
s2 += 4 * f(x);
}
integral = (h / 3) * (f(a) + s1 + s2 + f(b));
printf("Integral is %f",integral);
getch();
return 0;
}

Computer Programming and Numerical Methods 71


Output:

Run 1:

Enter a and b limits


2,4
Enter no. of steps :
2
Integral is 0.511111

Run 2:

Enter a and b limits


5,7
Enter no. of steps :
5
Integral is 0.270600

Computer Programming and Numerical Methods 72


14. Implement Gaussian quadrature for numerical integration

Flow Chart:

Computer Programming and Numerical Methods 73


Algorithm:

Step – 1 : Start

Step – 2 : Declare p, q, r, a, b, k as float variables

Step – 3 : Enter limits a and b

Step – 4 : Assign p = (a + b) / 2

Step – 5 : Assign q = (b – a) / 2

Step – 6 : Assign Integral = q *(f(p + q / sqrt(3))+f(p-q/sqrt(3)))

Step – 7 : Print integral value

Step – 8 : Stop

Step – 1 : Call function float f(float x)

Step – 2 : return(x * x + 2 * x)

Computer Programming and Numerical Methods 74


Program:

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

float f(float x)
{
//return(exp(-x*x));
return(x * x + 2 * x);
}

main()
{
float p, q, a, b, Integral;
clrscr();
printf("Enter a and b limits \n");
scanf("%f,%f",&a,&b);
p = (a + b) / 2;
q = (b - a) / 2;
Integral = q *(f(p + q / sqrt(3))+f(p-q/sqrt(3)));
printf("Integral = %f\n", Integral);
getch();
return 0;
}

Output:

Enter a and b limits


2, 4
Integral = 30.666666

Computer Programming and Numerical Methods 75


15. Write a program to solve a set of linear algebraic equations.

Flow Chart:

Computer Programming and Numerical Methods 76


Computer Programming and Numerical Methods 77
Algorithm:

Step – 1 : Start

Step – 2 : Declare variables i, j, k, n and array a[10][10]

Step – 3 : Enter the no. of unknowns

Step – 4 : Print enter coefficient of matrix

Step – 5 : Initialize i to zero

Step – 6 : Check whether i < n if true execute from Step–9 to Step–10 otherwise go to
Step–11

Step – 7 : Initialize j to zero

Step – 8 : Check whether i < n + 1 if true execute from Step–9 to Step–10 otherwise
increment by 1 and go to Step–6

Step – 9 : Read elements into an array

Step – 10 : Increment j by 1 and go to Step–8

Step – 11 : Initialize k to zero

Step – 12 : Check whether k < n if true execute from Step–13 to Step–20 otherwise go to
Step–21

Step – 13 : Initialize i to zero

Step – 14 : Check whether i < n if true execute from Step to Step otherwise increment r
and go to Step–12

Step – 15 : Check whether i == k if true go to Step–12 else go to Step–16

Step – 16 : Calculate r = a[i][k] / a[k][k]

Step – 17 : Initialize j to zero

Step – 18 : Check whether j < n + 1 if true go to next execute Step–19 to Step–20


otherwise increment by 1 and go to Step–14

Step – 19 : Calculate a[i][j] -=r * a[k][j]

Step – 20 : Increment i by 1 and go to Step–18

Computer Programming and Numerical Methods 78


Step – 21 : Initialize i to zero

Step – 22 : Check whether i < n if true execute from Step–23 to Step–24 else Step–26

Step – 23 : Calculate a[i][n] = a[i][n] / a[i][i]

Step – 24 : Print a[i][n]

Step – 25 : Increment i by 1 and go to Step–22

Step – 26 : Stop

Computer Programming and Numerical Methods 79


Program:

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

main()
{
int i, j, k, n;
float a[10][10], r;
clrscr();
printf("Enter the order of the matrix\n");
scanf("%d",&n);
printf("Enter the elements \n");
for(i = 0; i < n; i++)
for(j = 0; j < n + 1; j++)
scanf("%f",&a[i][j]);
for(k = 0; k < n; k++)
for(i = 0; i < n; i++)
{
if(i == k)
continue;
r = a[i][k] / a[k][k];
for(j = 0; j < n+1; j++)
a[i][j] -= r * a[k][j];
}
printf("The Solution is \n");
for(i = 0; i < n; i++)
{
a[i][n] = a[i][n] / a[i][i];
printf("%f\t",a[i][n]);
}
getch();
return 0;
}

Computer Programming and Numerical Methods 80


Output:

Run 1:

Enter the order of the matrix


2
Enter the elements
146824

The Solution is
0.133333 1.466667

Run 2:

Enter the order of the matrix


3
Enter the elements
135673573825

The Solution is
0.166667 0.318627 0.975490

Computer Programming and Numerical Methods 81

You might also like