Introduction to C programming Algorithms and flowcharts
Introduction to C programming Algorithms and flowcharts
LAB PROGRAMS
Algorithms and flowcharts
LAB PROGRAM 1
C Program to find Mechanical Energy of a particle using E = mgh+(1/2) mv^2
Algorithm
1. Start
2. Declare Variables:
• m (mass), h (displacement), v (velocity), p (potential energy), k (kinetic energy), e
(mechanical energy)
3. Input Mass:
• Print "Enter Mass of the body"
• Read value of m
4. Input Displacement:
• Print "Enter displacement of the body"
• Read value of h
5. Input Velocity:
• Print "Enter velocity of the body"
• Read value of v
6. Calculate Potential Energy:
• p = m * 9.8 * h
7. Calculate Kinetic Energy:
• k = 0.5 * m * (v * v)
8. Calculate Mechanical Energy:
• e = p + k
9. Output Results:
• Print "Potential energy of the body = p"
• Print "Kinetic energy of the body = k"
• Print "Mechanical energy of a body = e"
10.End
Flowchart
1. Start: Oval shape labeled "Start"
2. Input Section:
• Rectangle labeled "Input Mass (m)"
• Rectangle labeled "Input Displacement (h)"
• Rectangle labeled "Input Velocity (v)"
3. Calculation Section:
• Rectangle labeled "Calculate Potential Energy (p = m * 9.8 * h)"
• Rectangle labeled "Calculate Kinetic Energy (k = 0.5 * m * v^2)"
• Rectangle labeled "Calculate Mechanical Energy (e = p + k)"
4. Output Section:
• Rectangle labeled "Output Potential Energy (p)"
• Rectangle labeled "Output Kinetic Energy (k)"
• Rectangle labeled "Output Mechanical Energy (e)"
5. End: Oval shape labeled "End"
LAB PROGRAM 2
C Program to convert kilometre into meter, centimetre and millimetre
Algorithm
1. Start
2. Declare Variables:
• km (kilometers), m (meters), cm (centimeters), mm (millimeters)
3. Input Distance:
• Print "Enter the distance in Kilometre"
• Read the value of km
4. Convert Distance:
• Calculate m = km * 1000.0 (conversion to meters)
• Calculate cm = km * 100000.0 (conversion to centimeters)
• Calculate mm = km * 1000000.0 (conversion to millimeters)
5. Output Results:
• Print "Distance in meter is m"
• Print "Distance in centimeter is cm"
• Print "Distance in millimeter is mm"
6. End
LAB PROGRAM 5
Implement Matrix multiplication and validate the rules of multiplication
Algorithm
1. Start
2. Declare Variables:
• m, n (dimensions of the first matrix),
• p, q (dimensions of the second matrix),
• c, d, k (loop counters),
• sum initialized to 0.
• Arrays first[10][10], second[10][10], Multiply[10][10] for storing
the matrices.
3. Input Dimensions of First Matrix:
• Print "Enter number of rows and columns of first matrix"
• Read m and n.
4. Input Elements of First Matrix:
• Print "Enter elements of first matrix"
• For c = 0 to m:
• For d = 0 to n:
• Read first[c][d].
5. Input Dimensions of Second Matrix:
• Print "Enter number of rows and columns of second matrix"
• Read p and q.
6. Check Multiplication Condition:
• If n is not equal to p, print "The multiplication isn't possible."
7. Input Elements of Second Matrix:
• Print "Enter elements of second matrix"
• For c = 0 to p:
• For d = 0 to q:
• Read second[c][d].
8. Matrix Multiplication:
• For c = 0 to m:
• For d = 0 to q:
• For k = 0 to p:
• Calculate sum += first[c][k] * second[k][d]
• Set Multiply[c][d] = sum
• Reset sum to 0.
9. Output Result:
• Print "Product of the matrices:"
• For c = 0 to m:
• For d = 0 to q:
• Print Multiply[c][d].
10.End
LAB PROGRAM 6
Compute cos(x) using Taylor series approximation. Compare you result
with the built-in library function. Print both the results with appropriate inferences.
Algorithm
1. Start
2. Declare Variables:
• n (number of terms),
• x1 (original value of x),
• x (angle in radians),
• i, j (loop counters),
• sign (to alternate signs),
• cosx (cosine result),
• fact (factorial).
3. Input Number of Terms:
• Print "Enter the number of the terms in a series:"
• Read n.
4. Input Angle in Degrees:
• Print "Enter the value of x (in degrees):"
• Read x.
5. Convert Degrees to Radians:
• Store the original value of x in x1.
• Convert x to radians using x = x * (3.142 / 180.0).
6. Initialize Variables:
• Set cosx = 1 and sign = -1.
7. Calculate Cosine using Taylor Series:
• For i = 2 to n (incrementing by 2):
• Set fact = 1.
• For j = 1 to i:
• Calculate fact = fact * j.
• Update cosx = cosx + (pow(x, i) / fact) * sign.
• Update sign = sign * (-1).
8. Output Results:
• Print "Sum of the cosine series = cosx."
• Print "The value of cos(x1) using library function = cos(x)."
9. End
Creating a Flowchart
1. Start with the Start Symbol:
• Draw an oval at the top of your flowchart and label it "Start".
2. Input Number of Elements:
• Draw a parallelogram below the "Start" oval for inputting the number of elements
(n). Label it "Input number of elements (n)".
3. Input Sorting Order:
• Draw another parallelogram below the previous one for the sorting order. Label it
"Input sorting order (1 for Ascending, 2 for Descending)".
4. Input Array Elements:
• Draw a parallelogram for inputting the array elements. Label it "Input array
elements".
5. Decision: Check Sorting Order:
• Draw a diamond below the input for the array elements. Label it "Is order = 1?".
6. Branch for Ascending Order:
• From the diamond, draw an arrow for the Yes path:
• Draw a rectangle to represent the sorting logic for ascending order. Label it
"Sort in Ascending Order".
• Include nested loops here (you can summarize it in one rectangle if needed).
7. Output Sorted Array (Ascending):
• Draw a parallelogram below the sorting rectangle. Label it "Output sorted array in
ascending order".
8. Branch for Descending Order:
• Go back to the diamond and draw an arrow for the No path:
• Draw another diamond labeled "Is order = 2?".
• From this diamond, draw an arrow for the Yes path:
• Draw a rectangle for the sorting logic for descending order. Label it
"Sort in Descending Order".
• Include nested loops here as well.
9. Output Sorted Array (Descending):
• Draw a parallelogram below the descending sorting rectangle. Label it "Output
sorted array in descending order".
10.Handle Invalid Order:
• Go back to the diamond that checks if order is 2. Draw an arrow for the No path:
• Draw a parallelogram labeled "Output 'Invalid selection of order'".
11.End Symbol:
• Finally, draw an oval at the bottom and label it "End".
LAB PROGRAM 8
Write functions to implement string operations such as compare, concatenate,
string length. Convince the parameter passing techniques.
Steps to Create the Algorithm
1. Start
2. Declare variables:
• n for choice
• digit for continuation decision
• str1 and str2 for strings
3. Input first string (str1).
4. Input second string (str2).
5. Do-while Loop:
• Print menu options (compare, concatenate, length).
• Input choice (n).
• Switch statement on n:
• Case 1: Call compare(str1, str2) function.
• Case 2: Call concat(str1, str2) function.
• Case 3: Call length(str1) function.
• Default: Print "Wrong choice".
• Input digit to ask if the user wants to continue.
6. End
Creating the Flowchart
1. Start:
• Draw an oval labeled "Start".
2. Input Strings:
• Draw two parallelograms:
• First one labeled "Input first string (str1)".
• Second one labeled "Input second string (str2)".
3. Do-While Loop:
• Draw a rectangle for the start of the loop. Label it "Display menu options".
4. Input Choice:
• Draw a parallelogram labeled "Input choice (n)".
5. Decision for Choice:
• Draw a diamond labeled "Is n = 1?".
• Yes path:
• Draw a rectangle labeled "Call compare(str1, str2)".
• No path:
• Draw another diamond labeled "Is n = 2?".
• Yes path:
• Draw a rectangle labeled "Call concat(str1, str2)".
• No path:
• Draw another diamond labeled "Is n = 3?".
• Yes path:
• Draw a rectangle labeled "Call
length(str1)".
• No path:
• Draw a rectangle labeled "Print 'Wrong
choice'".
6. Ask to Continue:
• After handling the choice, draw a parallelogram labeled "Input digit (continue?)".
7. Decision to Continue:
• Draw a diamond labeled "Is digit = 1?".
• Yes path: Loop back to the start of the loop.
• No path: Draw an oval labeled "End".
LAB PROGRAM 9
Implement structures to read, write and compute average-marks and the
students scoring above and below the average marks for a class of N students.
Steps to Create the Algorithm
1. Start
2. Declare a struct named student with fields:
• usn (char array)
• name (char array)
• m1, m2, m3 (float for marks)
• avg, total (float for average and total scores)
3. Declare variables:
• s as an array of student structures.
• n for the number of students.
• i as a loop counter.
• tavg, sum for average and total calculation.
4. Input the number of students (n).
5. For each student from 0 to n-1:
• Input usn, name, and the three subject scores (m1, m2, m3).
• Calculate total = m1 + m2 + m3.
• Calculate avg = total / 3.
6. For each student from 0 to n-1:
• If avg >= 35:
• Print that the student scored above average.
• Else:
• Print that the student scored below average.
7. End
LAB PROGRAM 10
Develop a program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of N real numbers
Steps to Create the Algorithm
1. Start
2. Declare variables:
• n for the number of values
• i for loop counter
• x[20] as an array to store real values
• sum, mean, variance, deviation for calculations
3. Input the number of values (n).
4. Input n real values into array x.
5. Calculate Sum:
• Initialize sum = 0.
• For each element in x:
• Add the element to sum.
6. Calculate Mean:
• mean = sum / n.
7. Calculate Variance:
• Initialize sum = 0.
• For each element in x:
• Calculate (element - mean)² and add to sum.
• variance = sum / n.
8. Calculate Standard Deviation:
• deviation = sqrt(variance).
9. Output the values of sum, mean, variance, and standard deviation.
10.End