Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Introduction to C programming Algorithms and flowcharts

The document outlines several C programming lab programs, including algorithms and flowcharts for calculating mechanical energy, converting distances, checking character cases, balancing chemical equations, performing matrix multiplication, and computing sine and cosine using Taylor series. Each lab program includes a step-by-step algorithm and a corresponding flowchart creation guide. The content is aimed at teaching fundamental programming concepts and mathematical calculations through practical examples.

Uploaded by

angelotommy006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Introduction to C programming Algorithms and flowcharts

The document outlines several C programming lab programs, including algorithms and flowcharts for calculating mechanical energy, converting distances, checking character cases, balancing chemical equations, performing matrix multiplication, and computing sine and cosine using Taylor series. Each lab program includes a step-by-step algorithm and a corresponding flowchart creation guide. The content is aimed at teaching fundamental programming concepts and mathematical calculations through practical examples.

Uploaded by

angelotommy006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

INTRODUCTION TO C PROGRAMMING

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

Example Flowchart Creation


To visually create the flowchart:
• Start: Draw an oval and label it "Start."
• Input: Draw a rectangle below it and label it "Input Distance in Kilometers (km)."
• Conversions: Draw three rectangles below the input:
• "Convert to Meters (m = km * 1000.0)"
• "Convert to Centimeters (cm = km * 100000.0)"
• "Convert to Millimeters (mm = km * 1000000.0)"
• Output: Draw three rectangles below the conversion steps:
• "Output Distance in Meters (m)"
• "Output Distance in Centimeters (cm)"
• "Output Distance in Millimeters (mm)"
• End: Draw an oval at the bottom labeled "End."
LAB PROGRAM 3
C Program To Check the Given Character is Lowercase or Uppercase
Algorithm
1. Start
2. Declare Variable:
• c (character)
3. Input Character:
• Print "Enter any character: "
• Read the value of c
4. Check Character Case:
• If c is between 'A' and 'Z', then:
• Print "character is an upper case"
• Else if c is between 'a' and 'z', then:
• Print "character is a lower case"
• Else:
• Print "it is not a character"
5. End

Flowchart Creation Example


To create a flowchart based on the algorithm:
1. Start:
• Draw an oval shape and label it "Start."
2. Input Character:
• Draw a rectangle below it labeled "Input Character (c)."
3. First Decision:
• Draw a diamond below the input labeled "Is c >= 'A' and c <= 'Z'?"
• Draw an arrow labeled "Yes" leading to a rectangle that says "Output:
character is an upper case."
• Draw an arrow labeled "No" leading to the next decision.
4. Second Decision:
• Draw another diamond labeled "Is c >= 'a' and c <= 'z'?"
• Draw an arrow labeled "Yes" leading to a rectangle that says "Output:
character is a lower case."
• Draw an arrow labeled "No" leading to a rectangle that says "Output: it is not
a character."
5. End:
• Draw an oval at the bottom labeled "End."
LAB PROGRAM 4
Program to balance the given Chemical Equation values x, y, p, q of a simple chemical equation
of the type: The task is to find the values of constants b1, b2, b3 such that the equation is
balanced on both sides and it must be the reduced form.
Algorithm
1. Start
2. Define Function gcd(int a, int b):
• Initialize hcf to 0.
• For i from 1 to the minimum of a and b:
• If a % i == 0 and b % i == 0, set hcf to i.
• Return hcf.
3. Define Function balance(int x, int y, int p, int q):
• If p % x == 0 and q % y == 0:
• Set b1 = p / x
• Set b2 = q / y
• Set b3 = 1
• Else:
• Calculate b3 = x * y
• Calculate temp = gcd(p, gcd(q, b3))
• Set b1 = p / temp
• Set b2 = q / temp
• Set b3 = b3 / temp
• Print "The coefficients are b1 = b1, b2 = b2, b3 = b3"
4. In main():
• Declare variables k, l, m, n.
• Print "Enter the atomic nature of reactants x and y"
• Read k and l.
• Print "Enter the atomic nature of products p and q"
• Read m and n.
• Call balance(k, l, m, n).
5. End

Flowchart Creation Example


To create a flowchart based on the algorithm:
1. Start:
• Draw an oval shape and label it "Start."
2. Input Reactants:
• Draw a rectangle labeled "Input atomic nature of reactants x (k) and y (l)."
3. Input Products:
• Draw a rectangle labeled "Input atomic nature of products p (m) and q (n)."
4. Check Conditions:
• Draw a diamond labeled "Is p % x == 0 and q % y == 0?"
• If Yes:
• Draw a rectangle labeled "Set b1 = p / x, b2 = q / y, b3 = 1."
• Draw an arrow leading to the output step.
• If No:
• Draw a rectangle labeled "Set b3 = x * y."
• Draw a rectangle labeled "Calculate temp = gcd(p, gcd(q, b3))."
• Draw a rectangle labeled "Set b1 = p / temp, b2 = q / temp, b3 = b3 /
temp."
5. Output Coefficients:
• Draw a rectangle labeled "Output: The coefficients are b1 = b1, b2 = b2, b3 = b3."
6. End:
• Draw an oval labeled "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

Flowchart Creation Example


To create a flowchart based on the algorithm:
1. Start:
• Draw an oval shape and label it "Start."
2. Declare Variables:
• Draw a rectangle labeled "Declare Variables (m, n, p, q, c, d, k, sum)."
3. Input Dimensions of First Matrix:
• Draw a rectangle labeled "Input m and n (rows and columns of first matrix)."
4. Input Elements of First Matrix:
• Draw a rectangle labeled "Input elements of first matrix."
5. Input Dimensions of Second Matrix:
• Draw a rectangle labeled "Input p and q (rows and columns of second matrix)."
6. Check Multiplication Condition:
• Draw a diamond labeled "Is n equal to p?"
• If No: Draw a rectangle labeled "Output: The multiplication isn't possible."
• Draw an arrow to the "End" step.
• If Yes: Continue to the next step.
7. Input Elements of Second Matrix:
• Draw a rectangle labeled "Input elements of second matrix."
8. Matrix Multiplication:
• Draw a nested structure:
• Draw a rectangle for outer loop "For c = 0 to m"
• Inside it, draw another rectangle for inner loop "For d = 0 to q"
• Inside this, draw another rectangle for "For k = 0 to p"
• Draw a rectangle for "sum += first[c][k] * second[k][d]"
• Draw a rectangle for "Multiply[c][d] = sum"
• Draw a rectangle for "Reset sum to 0."
9. Output Result:
• Draw a rectangle labeled "Output: Product of the matrices."
10.End:
• Draw an oval labeled "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

Flowchart Creation Example


To create a flowchart based on the algorithm:
1. Start:
• Draw an oval shape and label it "Start."
2. Declare Variables:
• Draw a rectangle labeled "Declare Variables (n, x1, x, i, j, sign, cosx, fact)."
3. Input Number of Terms:
• Draw a rectangle labeled "Input n (number of terms)."
4. Input Angle in Degrees:
• Draw a rectangle labeled "Input x (angle in degrees)."
5. Convert Degrees to Radians:
• Draw a rectangle labeled "Convert x to radians (x = x * (3.142 / 180))."
• Draw a rectangle labeled "Store original x in x1."
6. Initialize Variables:
• Draw a rectangle labeled "Set cosx = 1, sign = -1."
7. Calculate Cosine:
• Draw a diamond labeled "For i = 2 to n (increment by 2)?"
• If No: Go to output results.
• If Yes: Continue.
• Draw a rectangle labeled "Set fact = 1."
• Draw another diamond labeled "For j = 1 to i?"
• If No: Go back to the main loop.
• If Yes: Draw a rectangle labeled "fact = fact * j."
• Draw a rectangle labeled "Update cosx = cosx + (pow(x, i) / fact) *
sign."
• Draw a rectangle labeled "Update sign = sign * (-1)."
8. Output Results:
• Draw a rectangle labeled "Output: Sum of the cosine series = cosx."
• Draw another rectangle labeled "Output: cos(x1) using library function = cos(x)."
9. End:
• Draw an oval labeled "End."
LAB PROGRAM 7
Compute sin(x) using Taylor series approximation. Compare you result with the
built-in library function. Print both the results with appropriate inferences. Sine
function using
Taylor Series.
Example Algorithm
Here’s the algorithm again, which you can use to create your flowchart:
1. Start
2. Input the number of terms for the series (number).
3. Input the angle x in degrees.
4. Convert x from degrees to radians using the formula: x=x×(π180)x = x \times \left(\frac{\pi}
{180}\right)
5. Initialize:
• term to x
• sinx to term
6. For each n from 1 to number:
• Calculate denominator as 2⋅n⋅(2⋅n+1)2 \cdot n \cdot (2 \cdot n + 1)
• Update term using:
term=−term⋅x⋅xdenominator\text{term} = -\text{term} \cdot \frac{x \cdot x}{\
text{denominator}}
• Update sinx as:
sinx=sinx+term\text{sinx} = \text{sinx} + \text{term}
7. Output the calculated sinx.
8. Calculate the sine of x using the library function sin(x) and output the result.
9. End

Creating the Flowchart


1. Draw an oval at the top labeled "Start".
2. Draw a parallelogram below it for the first input (number of terms).
3. Draw another parallelogram for the second input (value of x).
4. Draw a rectangle for the conversion of x to radians.
5. Draw another rectangle for the initialization of variables.
6. Draw a rectangle for the loop setup.
7. Inside the loop, use rectangles for calculating the denominator, updating the term, and
updating sinx.
8. After the loop, use a parallelogram for outputting sinx and another for calculating the library
function.
9. Draw an oval at the bottom labeled "End".
By following these steps, you can create a clear and structured flowchart that visually represents the
algorithm's logic.
Sort the given set of N numbers using Bubble-sort.
Example Algorithm
Here’s the algorithm for the sorting program:
1. Start
2. Input the number of elements (n).
3. Input the sorting order (order): 1 for ascending, 2 for descending.
4. Input the array elements.
5. If order is 1 (ascending):
• For each element from 0 to n-1:
• For each element from 0 to n-c-1:
• If array[d] > array[d + 1], swap them.
• Output the sorted array in ascending order.
6. Else If order is 2 (descending):
• For each element from 0 to n-1:
• For each element from 0 to n-c-1:
• If array[d] < array[d + 1], swap them.
• Output the sorted array in descending order.
7. Else (if the order is invalid):
• Output "Invalid selection of order."
8. 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

Creating the Flowchart


1. Start:
• Draw an oval labeled "Start".
2. Declare Struct and Variables:
• Draw a rectangle for declaring the student struct.
• Draw another rectangle for declaring s, n, i, tavg, and sum.
3. Input Number of Students:
• Draw a parallelogram labeled "Input number of students (n)".
4. For Loop for Student Details:
• Draw a rectangle for the start of the loop. Label it "For each student i from 0 to n-1".
• Inside this rectangle, add:
• Draw three parallelograms for inputting:
• "Input USN".
• "Input Name".
• "Input three subject scores (m1, m2, m3)".
• Draw two rectangles for calculating total and average:
• "Calculate total = m1 + m2 + m3".
• "Calculate avg = total / 3".
5. For Loop to Check Average:
• Draw another rectangle for the start of this loop. Label it "For each student i from 0
to n-1".
• Draw a diamond labeled "Is avg >= 35?".
• Yes path:
• Draw a rectangle labeled "Print student name scored above average".
• No path:
• Draw a rectangle labeled "Print student name scored below average".
6. End Symbol:
• Draw an oval at the bottom labeled "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

Creating the Flowchart


1. Start:
• Draw an oval labeled "Start".
2. Declare Variables:
• Draw a rectangle for declaring variables: n, i, x[20], sum, mean, variance,
and deviation.
3. Input Number of Values:
• Draw a parallelogram labeled "Input number of values (n)".
4. Input Values into Array:
• Draw a parallelogram labeled "Input n real values into array x".
5. Calculate Sum:
• Draw a rectangle for the start of the sum calculation loop. Label it "Initialize sum =
0".
• Draw a rectangle labeled "For each element in x, add to sum".
• Draw a rectangle labeled "Calculate mean = sum / n".
6. Calculate Variance:
• Draw a rectangle for the start of the variance calculation loop. Label it "Initialize
sum = 0".
• Draw a rectangle labeled "For each element in x, add (element - mean)² to sum".
• Draw a rectangle labeled "Calculate variance = sum / n".
7. Calculate Standard Deviation:
• Draw a rectangle labeled "Calculate deviation = sqrt(variance)".
8. Output Results:
• Draw four parallelograms for outputting:
• "Output sum".
• "Output mean".
• "Output variance".
• "Output standard deviation".
9. End Symbol:
• Draw an oval labeled "End".

You might also like