0% found this document useful (0 votes)
5 views

Programs Till Week 10

Uploaded by

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

Programs Till Week 10

Uploaded by

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

Practice coding

Loops practice (patterns and math)

Star Rectangle:

Stationary number square:

Printing an odd number triangle:


Practice coding

Alphabet square:

Star Plus pattern:

For Rhombus pattern:

For Star Pyramid:


Practice coding

Another pattern printing (corrected):


Practice coding

Inverted triangle:

Corrected full pyramid:


Practice coding

Full number pyramid (type 1):

For inverted 54321 pattern part 1:


Practice coding

Part 2:

Pattern involving * and number:


Practice coding

Program for the inverted version of the above:


Practice coding

Program for star right triangle:

Floyd’s triangle: Similar number pattern (exam question) :


Practice coding

Slot 1 pattern exam question:

Pattern involving 1 and 2 and 3 (exam question):

Alternating 1s and 0s triangle:


Practice coding

Pattern involving the sum of the terms resulting in the third:


Practice coding

Another similar pattern:

Program for diamond number pattern:


Practice coding

Another diamond star pattern:

Another diamond pattern:


Practice coding

Pattern resembling 7:
Practice coding

Another exam pattern:

Slight variation of a previous pattern:


Practice coding

Inverted pattern of the previous examples:

Doubt:

Pascal’s triangle (only works till rows=5):


Practice coding

Revised version of Pascal’s triangle:

Fibonacci Triangle:
Practice coding

Printing stars and # pattern (don’t know what those red things are):
Practice coding

For/If program (max and min values):

Program for: a^b


Practice coding

Finding the exponents using for loops (exam question):

Finding exponents using while loops (my discovery for flowcharts):

*Interesting math problem*:


Practice coding

Nested loops problem:


Practice coding

Another average problem:


Practice coding

Counting question:
Practice coding

Another average problem:


Practice coding

Loop Question:

t is for indenting to the right.

Max,min question:
Practice coding

Lol method: *need to ask*


Practice coding

Multiplication table (nested for loop):


Practice coding

Printing the series: 1+11+111+1111….:


Practice coding

Exam question:

Exam question:
Practice coding
Practice coding

Corrected form of the above code:

Determine whether n is prime using a for loop:


Practice coding

One variant of the Fibonacci series:

HCF of two numbers:

LCM using while loop:


Practice coding

LCM using for loop:


Practice coding

Finding if a value is prime in a given interval:

Finding prime numbers using a while loop and a for loop:

Finding the prime values in a given interval:


Practice coding

Functions

Q1:
Practice coding
Practice coding

Question 2:
Practice coding

Question 3:
Practice coding
Practice coding

Factorial of a number using functions:

For checking Palindrome*****: next page:


Practice coding

Armstrong number and perfect number:


Practice coding
Practice coding

Another functions question:


Practice coding

Printing prime numbers using functions:


Practice coding

Checking if a number can be written as the sum of two primes:


Practice coding

Addition using functions:

Swapping two variables without using a third variable (without pointers):


Practice coding

Finding the sum of a particular series:

Checking if a number is prime using functions:


Practice coding

Output of the following func:

Finding area, circum, diam using functions:

Finding the leading digit of a number:


Practice coding

Checking only Armstrong numbers:

Printing Armstrong numbers in an interval using for and while:


Practice coding

Checking only perfect numbers:


Practice coding

Perfect numbers using loops:

Finding the sum of the cubes in a particular interval using functions:

Celcius to Fahrenheit conversion:


Practice coding

Converting cm to feet and inches:

Unit conversions(extremely important, came in exam):


Practice coding

Autograded assignment Q2:


Practice coding
Practice coding

Checking strong numbers:

Strong numbers with restrictions (for loop immediately after scanning):

Arrays:
Practice coding

Question 1 (lab 1):

Question 3(lab 1):


Practice coding

Question 2(lab 1):

Solving avg using arrays:


Practice coding

Linear search using arrays:

Binary search algorithm:


Practice coding
Practice coding

Using sort (printing n-1 terms instead of n):

Modification of the previous code which makes it actually work:

Sorting and then finding the smallest missing element in the list (works when 1 is in the list):
Practice coding

Modification of previous which works always:


Practice coding

The smallest missing number in an unsorted array (only works if 1 is an element and can’t find any other
method):
Practice coding
Practice coding

Sorting a 2d array and then finding if an element is present in the matrix:


Practice coding

Printing an array with the elements being the result of multiplication barring arr[i]:
Practice coding

NOTE: 3*4*5*6*7=2520.

Printing elements of an array in descending order:


Practice coding
Practice coding

Left rotating and right rotating an array:


Practice coding

Swapping the elements of two arrays:

Printing the third smallest element using selection sort:


Practice coding

Finding number of +ve values using arrays:


Practice coding

Function for union of arrays (exam question):


Practice coding

Another union of arrays program:


Practice coding

Program to check if (2,3) exists consecutively:


Practice coding

Converting lowercase characters to uppercase characters:


Practice coding

Using printf and scanf instead:


Practice coding

Vowels in a string:

Average value of characters using arrays and strings:

Traversing an array:
Practice coding

Inserting an element at a desired position:

Deleting an element at a position:


Practice coding

Reversing an array:

Reversing an array using functions:

Merging two sorted arrays:


Practice coding

Merging two sorted arrays of the same size and then finding the median using user input:
Practice coding

Merging two sorted arrays of different sizes and then finding the median:
Practice coding
Practice coding

Checking duplicates:

Removing duplicates from an array:


Practice coding

Finding unique elements:

Extremely specific case for finding unique elements:

Unique numbers without using count:


Practice coding
Practice coding

Counting the frequency of each element in array:

Printing the elements which have an odd number of occurrences in the array:
Practice coding

Printing the repeated values in an array (slight modification of prev):


Practice coding
Practice coding

Finding maximum and minimum values using selection sort:


Practice coding

majority element in array:


Practice coding

Specific case for finding maximum sum of an array:

Finding the missing number in the array:


Practice coding

Printing the max value of an increasing then decreasing array:

Finding the ceil in arrays:


Practice coding

Generalized case (ceilsearch function remains the same, only a few modifications to the main function):

Finding if an array is a subset of another:


Practice coding
Practice coding

Making each element in the array gain the maximum value:

Replacing the array with the greatest elements to the right:

Pushing 0s to the end of the array:


Practice coding

Finding the max difference between any 2 elements of a given array:


Practice coding

Autograded assignment 1 question 1:


Practice coding

Autograded assignment 1, question 3:


Practice coding

Finding the value with the most frequency:


Practice coding

Finding the minimum distance between 2 elements in an array:


Practice coding

The minimum distance function with brackets:


Practice coding

Arranging an array with the smallest element printed before the largest element and so on:

Printing the number of clumps:


Practice coding
Practice coding

Concatenating two arrays into 1 combined array (uses malloc):


Practice coding

Printing largest span between repeating values:

Printing a 2D array (matrix) with user input:

Calculating the sum of the multiplication of the elements in a 2d array:


Practice coding

Matrix addition (same logic for subtraction):


Practice coding

Matrix multiplication: (next page):


Practice coding
Practice coding

Multiplying a square matrix with a column vector:

Transposing a matrix (using 2 matrices):

Transposing a matrix using only 1:


Practice coding

Checking sparse matrix:

Printing the right diagonal of the matrix:


Practice coding

Printing the sum of the rows and columns of a matrix:


Practice coding
Practice coding

Rotating a matrix by 180:


Practice coding

Swapping diagonals:
Practice coding

Rotating by 90:
Practice coding

Done the same but this time by flipping the matrix horizontally and vertically:
Practice coding

Rotating by 270:
Practice coding

The same for 270 but by flipping the matrix vertically and horizontally:
Practice coding

Checking if symmetric matrix (A=A^T):


Practice coding

Finding lowest sum of a column under constraints (maybe wrong but MS?):

Lower triangular matrix:

Upper triangular matrix:


Practice coding

Determinant of 3 x 3 matrix using the determinant formula:


Practice coding

Checking if a matrix is an identity matrix:

Finding the row with the maximum number of 1s in a n x n matrix:


Practice coding

Printing the individual letters from a string:


Practice coding

Eliminating spaces from an array:

Eliminating spaces using malloc:

Printing the number of words in a sentence:


Practice coding

Sorting string in ascending order:

Finding the word “the” in a string:


Practice coding

Converting string to integer:

Converting lowercase to uppercase:

Converting uppercase to lowercase:


Practice coding

Checking if a letter is uppercase or lowercase:

Printing stars to replace spaces in a string:


Practice coding

Checking hexadecimal or not:

Breaking a string into pieces (landmark- spaces) *didn’t understand this* :


Practice coding

Printing the frequency of characters in a string:

Changing the vowels in a string to uppercase:


Practice coding

Finding maximum occurring character in a string:

Extracting a substring from the original string:


Practice coding

Finding smallest and largest word M1 (not ideal):


Practice coding

\
Practice coding

Using selectionsort algorithm on strings:

Printing palindrome using strings:


Practice coding

Removing integers:

Printing full name and last name using strcat:


Practice coding

Concatenating without using string.h:


Practice coding

Concatenation with constraints (exam question- yielded an error):

Counting the number of alphabets, digits and special characters in a string:


Practice coding

Using the atof function:

Finding the smallest length:


Practice coding

Copying the strings that start with an alphabet to a string and another which doesn’t start with alpha:
Practice coding

Using strcmp to arrange words:


Practice coding

Structures

Basic example:

Sizeof operator on structures:


Practice coding

Practice Lab 1 Q1:

Practice Lab 1 Q2:


Practice coding

Practice Lab 1, Q3:

Nested structures example:


Practice coding

Array of structures:

Accessing structures in two different ways:

Practice Lab 2, Q1:


Practice coding

Structures involving cricketers (exam question):


Practice coding

More complex structure (exam question):


Practice coding

Practice Lab 2, Q2:


Practice coding
Practice coding

Practice Lab2 Q3: comparing dates:


Practice coding

Finding the quadrants of points:


Practice coding

Distance and collinearity using structures:

Printing the average marks in a structure in a given condition:


Practice coding

Finding the resultant time:

Calculating the difference in time (exam question):


Practice coding

Calculating the difference in time (without pointers and full code):


Practice coding

This time, with pointers:

Finding most expensive and least expensive book in a structure:


Practice coding
Practice coding

Finding the employee with the maximum salary (logic seems to work):
Practice coding

Same problem but this time with functions:

Area and perimeter in structures:

Finding the maximum area:


Practice coding

Difference between 2 dates(official solution):


Practice coding

Another way to approach:


Practice coding
Practice coding
Practice coding

Rental cost of cars:


Practice coding

Pointers

Relation between address and pointer:

Printing the address:

Printing address along with value:

Printing the sum using pass by reference:


Practice coding

Finding the greatest value using pointers:

Pointers and arrays:

Length of string using pointers:


Practice coding

Vowels and consonants using pointers:

Swapping numbers using pointers:


Practice coding

Factorial using pointers:

Sorting using pointers:


Practice coding

Sum of elements of an array:


Practice coding

Reversing an array:
Practice coding

Finding the maximum element of an array:

Printing alphabets (way 1):

Printing alphabets (way 2):

Structures and Pointers:


Practice coding

Another pointers and structures question:


Practice coding

Two ways of accessing a structure using pointers (has come twice in the exam):
Practice coding

Another structures and pointers question (exam):


Practice coding
Practice coding

Reversing a string using pointers:


Practice coding

Number of occurrences of a character in an array using pointers:


Practice coding

Occurrences of a character in an array with user input and the use of fgets:

Checking if a string is palindrome (using pointers):


Practice coding

Concatenating two strings with pointers and without strcat:

Copying one string to an empty string using pointers:


Practice coding

Checking if 2 strings are anagrams:

Swapping two numbers using pointers:


Practice coding

Number of characters between two address points:

Practice Lab 1, Q3:


Practice coding

Practice Lab 2 (only question but very difficult and long)*****


Practice coding
Practice coding
Practice coding
Practice coding
Practice coding

Pointers and structures:


Practice coding

Important pointer exam question (did not get it right):

B99 example (Practice Lab 2):


Practice coding
Practice coding
Practice coding

Implementing absolute functionality using call by value & reference:


Practice coding

Absolute functionality if two inputs are given:


Practice coding

Practice Lab 4, Q2:


Practice coding
Practice coding

Practice Lab 5(again tough):


Practice coding
Practice coding

Dynamically allocated memory

Creating a dynamically allocated array:

Using malloc:
Practice coding

Dynamic array example:

Dynamic array vs static array:

Copying arrays (pass by ref):


Practice coding

Copying arrays(pointers):

Finding average using Malloc:


Practice coding

Finding the greatest value of an array:


Practice coding

Creating calloc using malloc and free:


Practice coding

Array of student with max CGPA:

**Note: it should be “<” instead of “>”


Practice coding

Creating a dynamically allocated 2D array:


Practice coding

Pizza shop structure problem (I swear I will break the laptop if such questions come):
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding

Text files in C

Creating a text file and inputting some data into it:

Finding if a string is a substring in a text file:


Practice coding

Reading from a file and then printing the content of that file:

Creating a txt file, inputting data and then reading from it:
Practice coding

Creating a new file and using fputs enter something into the new file:

Copying from 1 file to another:


Practice coding

Writing a program to read multiple lines from a file:

Reading a file and then storing in an array and then printing the contents:
Practice coding

Calculating the number of lines in a txt file:

Printing the no of words and characters in a txt file:


Practice coding

Replacing a line with another line in a txt file:


Practice coding
Practice coding

Copying one file to another file:


Practice coding

Merging two txt files into a third txt file:


Practice coding

Reading from 2 files and getting a result from then and placing it in a 3rd file:
Practice coding

Similar problem in exam:


Practice coding

Creating two files to store max and min value of a text file containing random numbers:
Practice coding

Miscellaneous

Creating a student database:


Practice coding
Practice coding

Week 7 lab assignment:

Planetary Colonisation Evaluation Module


Write a program to implement a planetary colonization evaluation module using a structure array, for
space scientists who are evaluating the suitability of other planets for human colonization. This program
should be capable of handling the following operations: The program should prompt the user to enter
the number of planets. This will determine the size of the array of structures. Each structure should have
the following fields:
planetName (array of characters) to store the name of the planet
, distanceFromEarth (float) for the distance of the planet from Earth, in light-years,
gravity (float) for the planet's gravity, as a factor of Earth’s gravity ‘g’ (1.2 implies 1.2g, 0.5 implies 0.5g,
etc)
atmosphere (a nested structure) that includes oxygen level, carbon dioxide level, and nitrogen level in
percentages (each stored as a float).
habitable (character) which is automatically evaluated by the program; a planet is habitable if its gravity
is between 0.8 and 1.2 g (both inclusive) and its oxygen level is between 18% and 25% (both inclusive).
This field should store whether the planet is "Y" (Yes) or "N" (No) for habitability, thus.
The program should present a menu to the user with the following options:
a) Find and print all habitable planets, sorted by their distance from Earth in ascending .
b) Search for and print the names of all planets where the oxygen level is above a user-given threshold
and the carbon dioxide level is below another user-given threshold.
c) Search for planets by a keyword or substring in their name (the keyword will be provided by the user)
and print the planet names that match.
d) Exit the program. The program should continue to display the menu after each operation until the
user chooses to exit. Important considerations for the operations: Assume that the planet name consists
of just one word, and there are no spaces in the name. The menu and other formatting has already been
provided to you in the template code. Only write in the space provided to you (denoted by the
instructions in comments). When sorting planets by distance, use the Selection Sort algorithm. When
searching by keyword or substring in planet names, use the strstr function to match the keyword. If the
user enters an invalid option, the program should display an "Invalid input" message. Please go through
the test case given below to design your MENU and understand how the expected outputs should be
printed. Testcase Enter the number of planets: 3 Enter Planet Name: Mercury Enter Distance from Earth
(in light-years): 4.3 Enter Planet Gravity (in terms of g): 1.15 Enter Atmosphere Composition (Oxygen,
Carbon Dioxide, Nitrogen) as percentages: 19 0.03 75 Enter Planet Name: Earth Enter Distance from
Earth (in light-years): 0 Enter Planet Gravity (in terms of g): 1 Enter Atmosphere Composition (Oxygen,
Carbon Dioxide, Nitrogen) as percentages: 21 0.02 78 Enter Planet Name: Neptune Enter Distance from
Earth (in light-years): 12 Enter Planet Gravity (in terms of g): 0.7 Enter Atmosphere Composition
(Oxygen, Carbon Dioxide, Nitrogen) as percentages: 16 0.05 80 MENU 1. Find and print all habitable
planets 2. Search by oxygen and carbon dioxide levels 3. Search by keyword in planet names 4. Exit 1
Habitable planets (sorted by distance from Earth): Earth - 0.00 light-years Mercury - 4.30 light-years
MENU 1. Find and print all habitable planets 2. Search by oxygen and carbon dioxide levels 3. Search by
keyword in planet names 4. Exit 3 Enter a keyword to search in planet names: Ear Planets matching the
keyword "Ear": Earth MENU 1. Find and print all habitable planets 2. Search by oxygen and carbon
dioxide levels 3. Search by keyword in planet names 4. Exit 4 Exiting...
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding
Practice coding

Guessing a secret word:


Practice coding

Bitwise operator question (exam):

Operator precedence and associativity:


Practice coding
Practice coding

Functions related to strings and characters:


Practice coding
Practice coding

Float and double range for displaying in C:

You might also like