CS112 Programming Languages 1 (Lecture 11 - Spring 2020)
CS112 Programming Languages 1 (Lecture 11 - Spring 2020)
Programming Languages 1
( General Division – Software Engineering Program – Medical Informatics Program )
Lecture 11
Revision; Basic / Simple Solved Examples [ 3 ]
3
11.1: Write a C Program to Calculate ..
Permutation (nPr) .. and .. Combination (nCr)
nC and nPr are statistical quantities regarding possible subsets of a
r
set of objects, and they are defined as:
nP ( n, r ) = n! / ( n − r )!
r
nC ( n, r ) = nP ( n, r ) / r! = n! / ( r! ( n − r )! )
r r
This result of 12 sets is the calculation of nPr( 4, 2), the total number
of permutations of 4 objects taking 2 at a time. We can confirm that
the formula produces 12:
nP ( 4, 2) = 4! / ( 4 − 2 )! = ( 4 ∗ 3 ∗ 2 ∗ 1 ) / ( 2 ∗ 1 ) = 24 / 2 = 12
r
11.1: Write a C Program to Calculate ..
Permutation (nPr) .. and .. Combination (nCr)
Example: Take the set { 1, 2, 3, 4} and find all combinations and
permutations taking 2 at a time:
The matrix is :
1 2
3 4
The transpose of the matrix is :
1 3
2 4
#include <stdio.h>
int main(void)
{
int arr1[ 50 ][ 50 ], brr1[ 50 ][ 50 ], i, j, r, c;
printf("\nInput the number of rows and columns of the matrix : ");
scanf("%d %d", &r, &c);
printf("Input the elements of the first matrix :\n");
for( i = 0; i < r; i++ )
{
for( j = 0; j < c; j++ )
{
printf("Element - [%d], [%d] : ", i, j);
scanf("%d", &arr1[ i ][ j ] );
}
}
printf("\nThe matrix is :\n");
for( i = 0; i < r; i++ )
{
printf("\n");
for( j = 0; j < c; j++ )
printf("%d\t", arr1[ i ][ j ] );
}
for( i = 0; i < r; i++ )
{
for( j = 0; j < c; j++ )
{
brr1[ j ][ i ] = arr1[ i ][ j ];
}
}
int main()
{
int arr1[] = { 0, 1, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0 };
int n = sizeof( arr1 ) / sizeof( arr1[ 0 ] );
int i;
//------------- Print the Original Array ------------------
printf("The Given Array is :\n\n");
for( i = 0; i < n; i++ ) { printf("%d ", arr1[ i ] ); } printf("\n\n\n");
//-------------- Print the Sorted Array ------------------
sortElements( arr1, n - 1 );
printf("After Sorting, the Elements of the Array are: \n\n");
for( i = 0; i < n; i++ ) { printf("%d ", arr1[ i ] ); } printf("\n\n\n");
return 0;
}
void swap( int arr1[], int i, int j )
{
int tmp = arr1[ i ];
arr1[ i ] = arr1[ j ];
arr1[ j ] = tmp;
}
Tracing:
void sortElements( int arr1[], int end )
{ { 0, 1, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0 } | start = 0, mid = 0, end = 11
int start = 0, mid = 0;
int pivot = 1; { 0, 1, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0 } | start = 1, mid = 1, end = 11
while( mid <= end )
{ start = 1, mid = 2, end = 11
if ( arr1[ mid ] < pivot )
{ { 0, 1, 0, 2, 1, 0, 0, 2, 0, 1, 1, 2 } | start = 1, mid = 2, end = 10
swap( arr1, start, mid );
{ 0, 0, 1, 2, 1, 0, 0, 2, 0, 1, 1, 2 } | start = 2, mid = 3, end = 10
++start, ++mid;
} { 0, 0, 1, 1, 1, 0, 0, 2, 0, 1, 2, 2 } | start = 2, mid = 3, end = 9
else if ( arr1[ mid ] > pivot )
{ start = 2, mid = 4, end = 9
swap( arr1, mid, end );
--end; start = 2, mid = 5, end = 9
}
{ 0, 0, 0, 1, 1, 1, 0, 2, 0, 1, 2, 2 } | start = 3, mid = 6, end = 9
else
{ { 0, 0, 0, 0, 1, 1, 1, 2, 0, 1, 2, 2 } | start = 4, mid = 7, end = 9
++mid;
} { 0, 0, 0, 0, 1, 1, 1, 1, 0, 2, 2, 2 } | start = 4, mid = 7, end = 8
}
} { 0, 0, 0, 0, 1, 1, 1, 1, 0, 2, 2, 2 } | start = 4, mid = 8, end = 8
// Check if the total number of bytes (of both files) are equal
if( cnt1 != cnt2 ) { printf("\nThe size of both files is not the same.\n"); }
else // Check if the contents of both files are the same
{
fseek( fp1, 0, SEEK_SET );
fseek( fp2, 0, SEEK_SET );
while( ! feof( fp1 ) )
{
if( fgetc( fp1 ) != fgetc( fp2 ) )
{
flg = 1;
break;
}
}
if( flg ) printf("\nThe contents of both files are NOT the same.\n");
else printf("\nThe contents of both files are the same.\n");
}
fclose( fp1 ); fclose( fp2 );
return 0;
}
11.7: Write a C Program to Read and Print a
Student’s Details using a Structure Pointer.
#include <stdio.h>
struct student{
char name[30];
int id; There are two ways of accessing members of structure using pointer:
float perc;
}; - Using arrow (->) operator or membership operator.
int main() - Using indirection (*) operator and dot (.) operator.
{
struct student std; // Structure Variable
struct student *ptr; // Pointer to a Student Structure
ptr = &std; // Assigning a value to the Pointer (the Address of a Structure Variable)
printf("Enter the Details of a Student: ");
printf("\nName? :"); gets( ptr->name );
printf("ID No.? :"); scanf("%d", &ptr->id );
printf("Percentage? :"); scanf("%f", &ptr->perc );
printf("\nThe Entered details are: \n");
printf("Name: %s\nID. No: %d \nPercentage: %.02f\n", ptr->name, ptr->id, ptr->perc );
return 0;
}
An Alternative Solution ..
#include <stdio.h>
struct student{
char name[30];
int id; There are two ways of accessing members of structure using pointer:
float perc;
}; - Using arrow (->) operator or membership operator.
int main() - Using indirection (*) operator and dot (.) operator.
{
struct student std; // Structure Variable
struct student *ptr; // Pointer to a Student Structure
ptr = &std; // Assigning a value to the Pointer (the Address of a Structure Variable)
printf("Enter the Details of a Student: ");
printf("\nName? :"); gets( (*ptr).name );
printf("ID No.? :"); scanf("%d", &(*ptr).id );
printf("Percentage? :"); scanf("%f", &(*ptr).perc );
printf("\nThe Entered details are: \n");
printf("Name: %s\nID. No: %d \nPercentage: %.02f\n", (*ptr).name,(*ptr).id,(*ptr).perc );
return 0;
}
If solved without using pointers .. ?
#include <stdio.h>
struct student{
char name[30];
int id;
float perc;
}; For accessing the members of a structure, use the dot/member (.) operator.
int main()
{
struct student std; // Structure Variable
printf("Enter the Details of a Student: ");
printf("\nName? :"); gets( std.name );
printf("ID No.? :"); scanf("%d", &std.id );
printf("Percentage? :"); scanf("%f", &std.perc );
printf("\nThe Entered details are: \n");
printf("Name: %s\nID. No: %d \nPercentage: %.02f\n", std.name, std.id, std.perc );
return 0;
}
11.8: Write a C Program to Read an Entire
String – with whitespace(s) – using scanf()
versus gets().
scanf()
- It is used to read the input (a character, a string, numeric data) from
#include <stdio.h> the standard input (keyboard).
int main() - It is used to read the input until it encounters a whitespace, a
{ newline, or End Of File (EOF).
char str[20];
return 0;
}
11.8: Write a C Program to Read an Entire
String – with whitespace(s) – using scanf()
versus gets().
gets()
- It is used to read the input (a string) from the standard input
#include <stdio.h> (keyboard).
int main() - It is used to read the input until it encounters a newline or End Of
{ File (EOF).
char str[20];
return 0;
}
11.8: Write a C Program to Read an Entire
String – with whitespace(s) – using scanf()
versus gets().
So, how to read a complete sentence from a user using scanf() .. ?
By using %[^\n]s inside the scanf() to read an entire string.
#include <stdio.h> The \n indicates that the input is read until a newline is encountered.
int main()
{
char str[200];
printf("Enter a string: ");
scanf("%[^\n]s", str);
printf("You entered: %s\n", str);
return 0;
}
11.9: Regarding Command-Line Arguments ..
What is the output of the following C Program
for each of the given cases:
// C program to illustrate Command-Line Arguments
#include<stdio.h>
C:\Users\Admin˃MyProgram.exe
int main( int argc, char* argv[] )
C:\Users\Admin˃MyProgram.exe First Second Third
{
C:\Users\Admin˃MyProgram.exe “First Second Third”
int counter;
printf(" Program Name Is: %s", argv[0] );
if( argc == 1 )
printf("\n No Extra Command-Line Arguments Passed Other Than the Program's Name");
if( argc >= 2 )
{
printf("\n Number Of Arguments Passed: %d", argc );
printf("\n ---- Following Are The Command-Line Arguments Passed ----");
for( counter = 0; counter < argc; counter++ )
printf("\n argv[ %d ]: %s", counter, argv[ counter ] );
}
return 0;
}
C:\Users\Admin˃MyProgram.exe
Program Name Is: MyProgram.exe
No Extra Command-Line Arguments Passed Other Than the Program's Name
31