Sunday 1 May 2022

Slection Sort

Selection sort theory  :

Steps involved in Selection Sort:

1. Find the smallest element in the array and swap it with the first element of the array i.e. a[0].
2. The elements left for sorting are n-1 so far. Find the smallest element in the array from index 1 to n-1 i.e. a[1] to a[n-1] and swap it with a[1].
3. Continue this process for all the elements in the array until we get a sorted list.



Program : 


/******************************************************************************

Online C++ Compiler.

Code, Compile, Run and Debug C++ program online.

SELECTION SORT

*******************************************************************************/


#include <iostream>

using namespace std;

// C++ program for implementation of selection sort

#include <bits/stdc++.h>

using namespace std;


//swap function 

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}


// SELECTION SORT FUNCTION

void selectionSort(int arr[], int n)

{

int i, j, min_idx;


// One by one move boundary of unsorted subarray

for (i = 0; i < n-1; i++)

{

// Find the minimum element in unsorted array

min_idx = i;

for (j = i+1; j < n; j++)

if (arr[j] < arr[min_idx])

min_idx = j;


// Swap the found minimum element with the first element

swap(&arr[min_idx], &arr[i]);

}

}


/* Function to print an array */

void printArray(int arr[], int size)

{

int i;

for (i=0; i < size; i++)

cout << arr[i] << " ";

cout << endl;

}


// Main Function

int main()

{

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr)/sizeof(arr[0]);

selectionSort(arr, n);

cout << "Sorted array: \n";

printArray(arr, n);

return 0;

}


// Time Complexity: O(n^2) n Squire 



Program Link: https://onlinegdb.com/9pM7uP0lL



Thursday 15 April 2021

Student DataBase Using c

Assignment : 1. Create Menu Driven Program for Stud Database System Using Structure.

Strucature Notes : All in 1 page


Structure :

Structure in c is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member.

The struct keyword is used to define the structure.

Friday 26 March 2021

Functions in C Programming

 A function is a block of statements that performs a specific task. Let’s say you are writing a C program and you need to perform a same task in that program more than once. 

Thursday 4 March 2021

Lec 5: C if...else Statements all

C if Statement

The syntax of the if statement in C programming is:

if (test expression) 
{
   // statements to be executed if the test expression is true
}

How if statement works?

The if statement evaluates the test expression inside the parenthesis ().

  • If the test expression is evaluated to true, statements inside the body of if are executed.
  • If the test expression is evaluated to false, statements inside the body of if are not executed

Example 1: if statement

// Program to display a number if it is negative

#include <stdio.h>
int main() {
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }

    printf("The if statement is easy.");

    return 0;
}

Output 1

Enter an integer: -2
You entered -2.
The if statement is easy.

When the user enters -2, the test expression number<0 is evaluated to true. Hence, You entered -2 is displayed on the screen.

Output 2

Enter an integer: 5
The if statement is easy.

When the user enters 5, the test expression number<0 is evaluated to false and the statement inside the body of if is not executed


C if...else Statement

The if statement may have an optional else block. The syntax of the if..else statement is:

if (test expression) {
    // statements to be executed if the test expression is true
}
else {
    // statements to be executed if the test expression is false
}

How if...else statement works?

If the test expression is evaluated to true,

  • statements inside the body of ifare executed.
  • statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,

  • statements inside the body of else are executed
  • statements inside the body of ifare skipped from execution.

Example 2: if...else statement

// Check whether an integer is odd or even

#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    // True if the remainder is 0
    if  (number%2 == 0) {
        printf("%d is an even integer.",number);
    }
    else {
        printf("%d is an odd integer.",number);
    }

    return 0;
}


Output

Enter an integer: 7
7 is an odd integer.

When the user enters 7, the test expression number%2==0 is evaluated to false. Hence, the statement inside the body of else is executed.


C if...else Ladder

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.

The if...else ladder allows you to check between multiple test expressions and execute different statements.


Syntax of if...else Ladder

if (test expression1) {
   // statement(s)
}
else if(test expression2) {
   // statement(s)
}
else if (test expression3) {
   // statement(s)
}
.
.
else {
   // statement(s)
}

Nested if...else

It is possible to include an if...elsestatement inside the body of another if...else statement.


Example 4: Nested if...else

This program given below relates two integers using either <> and =similar to the if...else ladder's example. However, we will use a nested if...else statement to solve this problem.

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    if (number1 >= number2) {
      if (number1 == number2) {
        printf("Result: %d = %d",number1,number2);
      }
      else {
        printf("Result: %d > %d", number1, number2);
      }
    }
    else {
        printf("Result: %d < %d",number1, number2);
    }

    return 0;
}


If the body of an if...elsestatement has only one statement, you do not need to use brackets {}.

For example, this code

if (a > b) {
    print("Hello");
}
print("Hi");

is equivalent to

if (a > b)
    print("Hello");
print("Hi");





Thursday 18 February 2021

Lec3: Data Types in C

 

Data Types in C

C has various data types to store data in program. C program can store integer, decimal number, character(alphabets), string(words or sentence), list etc. using various data types.


We need to specify the data type of variable(identifier) to store any data in it.


1. Primitives (Primary) Data Types

These data types store fundamental data used in the C programming.

  1. 1.int
  2. It is used to store integer values. C program compiled with GCC compiler (32-Bit) can store integers from -2147483648 to 2147483647. The size of int is compiler dependent. It takes 4 bytes in a 32-bit compiler such as GCC.

    intmyIntegerValue = 100;

  1. char
  2. It stores single character such as ‘a’, ‘Z’, ‘@’ etc. including number, symbol or special character. It takes 1 byte (8-bits) to store each character.

    charmyCharacter = 'A';

    Note: Every character has a corresponding ASCII value to it ranging from -128 to 127. Numbers as a character has their corresponding ASCII values too. For example, ‘1’ as char has ASCII value 49, ‘A’ has ASCII value 65.

  3. float
  4. It stores real numbers with precision upto 6 decimal places. It takes 4 bytes of memory and is also known as floating point number.

    floatmyFloatingValue = 100.6543;
  5. double
  6. It stores real numbers with precision upto 15 decimal places. It takes 8 bytes of memory.

    doublemyDoubleValue = 180.715586;


———————

Modifiers in C

These are keywords in C to modify the default properties of int and char data types. There are 4 modifiers in C as follows.

Modifiers In C

Modifiers In C

  1. short
  2. It limits user to store small integer values from -32768 to 32767. It can be used only on intdata type.

    shortintmyShortIntegerValue = 18;
  3. long
  4. It allows user to stores very large number (something like 9 Million Trillion) from -9223372036854775808 to 9223372036854775807. Syntax “long long” is used instead of “long int”.

    longlongmyLongIntegerValue = 827337203685421584;
  5. signed
  6. It is default modifier of int and char data type if no modifier is specified. It says that user can store negative and positive values.

    signedintmyNegativeIntegerValue = -544;signedintmypositiveIntegerValue = 544;/* Both of the statements have same meaning even without "signed" modifier*/
  7. unsigned
  8. When user intends to store only positive values in the given data type (int and char).

    unsignedintmyIntegerValue = 486;

———————-

Suze of program: