using C programming: 1-Create a massive text file of all codes (with documented
ID: 3712519 • Letter: U
Question
using C programming:
1-Create a massive text file of all codes (with documented comments) in Word (with Table of Contents) and in textfile so you don’t have formatting issues with any code)
Draw upon your own work, hand-outs, and Internet
Emphasis:
Sorting:
Numbers, letters, strings or ASCII
Ascending/descending
Frequency
Data validation:
Checking for letters, numbers, strings or ASCII
Formatting:
Printf
Calendars, names, patterns
Scanf
Multiple runs
Decision-making:
IF
IF/ELSE
SWITCH
&& ||
Functions
Loops:
FOR
WHILE
Stdin versus File
2. Create a word file of all definitions or critical information
Example: key word listing or special symbols like &&
Explanation / Answer
Sorting:
Numbers -
// C program for implementation of Bubble sort
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Letters -
Strings:-
#define MAX 100
void sortStrings(char arr[][MAX], int n)
{
char temp[MAX];
int j,i;
// Sorting strings using bubble sort
for (j=0; j<n-1; j++)
{
for (i=j+1; i<n; i++)
{
if (strcmp(arr[j], arr[i]) > 0)
{
strcpy(temp, arr[j]);
strcpy(arr[j], arr[i]);
strcpy(arr[i], temp);
}
}
}
}
int main()
{
int i,j;
char arr[][MAX] = {"GeeksforGeeks","Quiz","Practice","Gblogs","Coding"};
int n = sizeof(arr)/sizeof(arr[0]);
sortStrings(arr, n);
printf("Strings in sorted order are : ");
for (i=0; i<n; i++)
printf(" String %d is %s", i+1, arr[i]);
return 0;
}
Frequency-
Data validation:-
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c);
if( (c>='a' && c<='z') || (c>='A' && c<='Z'))
printf("%c is an alphabet.",c);
else if (c >= '0' && c <= '9')
printf("%c is an digit.",c);
else
printf("The ASCII Code for the character is: %d", c);
return 0;
}
Formatting:-
#include<stdio.h>
#include <time.h>
int main()
{
printf("The color: %s ", "blue");
printf("First number: %d ", 12345);
printf("Second number: %04d ", 25);
printf("Third number: %i ", 1234);
printf("Float number: %3.2f ", 3.14159);
printf("Hexadecimal: %x ", 255);
printf("Octal: %o ", 255);
printf("Unsigned value: %u ", 150);
printf("Just print the percentage sign %% ", 10);
printf(" ");
printf(":%s: ", "Hello, world!");
printf(":%15s: ", "Hello, world!");
printf(":%.10s: ", "Hello, world!");
printf(":%-10s: ", "Hello, world!");
printf(":%-15s: ", "Hello, world!");
printf(":%.15s: ", "Hello, world!");
printf(":%15.10s: ", "Hello, world!");
printf(":%-15.10s: ", "Hello, world!");
printf(" ");
time_t timer;
char buffer[26];
struct tm* tm_info;
time(&timer);
tm_info = localtime(&timer);
strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
puts(buffer);
return 0;
}
Scanf - Multiple runs
#include<malloc.h>
#include<stdio.h>
#include<conio.h>
int main(){
system("cls");
int a,b,c;
printf(" Enter 3 Numbers:");
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
printf(" Numbers Are: %d %d %d",a,b,c);
system("PAUSE");
return 0;
}
Decision-making:-
IF
IF-ELSE:-
SWITCH:-
&& ||:-
Functions:-
FOR LOOP:-
int main()
{
for (int i = 1; i <= 10; i++)
{
printf("Hello World ");
}
return 0;
}
WHILE LOOP:-
Please let me know in case of any clarifications required. Thanks!
// C program for implementation of Bubble sort
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
// Driver program to test above functions
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Letters -
#define CLASS_SIZE 10 #include <stdio.h> void bubbleSortAWriteToB(const char a[], char * b[]); int main(void){ int i; // initialize array char * s_letters[CLASS_SIZE]; char letters[CLASS_SIZE] = {'a','r','p','b','r','c','x','e','w','j'}; // sort array bubbleSortAWriteToB(letters,s_letters); // print sorted array for (i=0;i<CLASS_SIZE;i++){ printf("%c ", *s_letters[i]); } return 0; } void bubbleSortAWriteToB(const char a[], char * b[]){ char * temp; int i,j; // initialize b array to hold pointers to each element in a for (i=0;i<CLASS_SIZE;i++){ b[i] = (char *)(a) + i; } // in-place sort the b array for(i=0;i<CLASS_SIZE;i++){ for(j=i+1;j<CLASS_SIZE-1;j++){ if(*b[j-1]>*b[j]){ temp = b[j]; b[j] = b[j-1]; b[j-1] = temp; } } } } Strings:-
#define MAX 100
void sortStrings(char arr[][MAX], int n)
{
char temp[MAX];
int j,i;
// Sorting strings using bubble sort
for (j=0; j<n-1; j++)
{
for (i=j+1; i<n; i++)
{
if (strcmp(arr[j], arr[i]) > 0)
{
strcpy(temp, arr[j]);
strcpy(arr[j], arr[i]);
strcpy(arr[i], temp);
}
}
}
}
int main()
{
int i,j;
char arr[][MAX] = {"GeeksforGeeks","Quiz","Practice","Gblogs","Coding"};
int n = sizeof(arr)/sizeof(arr[0]);
sortStrings(arr, n);
printf("Strings in sorted order are : ");
for (i=0; i<n; i++)
printf(" String %d is %s", i+1, arr[i]);
return 0;
}
Frequency-
#include <stdio.h> #include <string.h> int main() { char string[100]; int c = 0, count[26] = {0}, x; printf("Enter a string "); gets(string); while (string[c] != '') { /** Considering characters from 'a' to 'z' only and ignoring others. */ if (string[c] >= 'a' && string[c] <= 'z') { x = string[c] - 'a'; count[x]++; } c++; } for (c = 0; c < 26; c++) printf("%c occurs %d times in the string. ", c + 'a', count[c]); return 0; } Data validation:-
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c);
if( (c>='a' && c<='z') || (c>='A' && c<='Z'))
printf("%c is an alphabet.",c);
else if (c >= '0' && c <= '9')
printf("%c is an digit.",c);
else
printf("The ASCII Code for the character is: %d", c);
return 0;
}
Formatting:-
#include<stdio.h>
#include <time.h>
int main()
{
printf("The color: %s ", "blue");
printf("First number: %d ", 12345);
printf("Second number: %04d ", 25);
printf("Third number: %i ", 1234);
printf("Float number: %3.2f ", 3.14159);
printf("Hexadecimal: %x ", 255);
printf("Octal: %o ", 255);
printf("Unsigned value: %u ", 150);
printf("Just print the percentage sign %% ", 10);
printf(" ");
printf(":%s: ", "Hello, world!");
printf(":%15s: ", "Hello, world!");
printf(":%.10s: ", "Hello, world!");
printf(":%-10s: ", "Hello, world!");
printf(":%-15s: ", "Hello, world!");
printf(":%.15s: ", "Hello, world!");
printf(":%15.10s: ", "Hello, world!");
printf(":%-15.10s: ", "Hello, world!");
printf(" ");
time_t timer;
char buffer[26];
struct tm* tm_info;
time(&timer);
tm_info = localtime(&timer);
strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
puts(buffer);
return 0;
}
Scanf - Multiple runs
#include<malloc.h>
#include<stdio.h>
#include<conio.h>
int main(){
system("cls");
int a,b,c;
printf(" Enter 3 Numbers:");
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
printf(" Numbers Are: %d %d %d",a,b,c);
system("PAUSE");
return 0;
}
Decision-making:-
IF
int main () { /* local variable definition */ int a = 10; /* check the boolean condition using if statement */ if( a < 20 ) { /* if condition is true then print the following */ printf("a is less than 20 " ); } printf("value of a is : %d ", a); return 0; } IF-ELSE:-
#include <stdio.h> int main () { /* local variable definition */ int a = 100; /* check the boolean condition */ if( a < 20 ) { /* if condition is true then print the following */ printf("a is less than 20 " ); } else { /* if condition is false then print the following */ printf("a is not less than 20 " ); } printf("value of a is : %d ", a); return 0; } SWITCH:-
#include <stdio.h> int main () { /* local variable definition */ char grade = 'B'; switch(grade) { case 'A' : printf("Excellent! " ); break; case 'B' : case 'C' : printf("Well done " ); break; case 'D' : printf("You passed " ); break; case 'F' : printf("Better try again " ); break; default : printf("Invalid grade " ); } printf("Your grade is %c ", grade ); return 0; } && ||:-
#include <stdio.h> int main() { int a = 5; int b = 20; int c ; if ( a && b ) { printf("Line 1 - Condition is true " ); } if ( a || b ) { printf("Line 2 - Condition is true " ); } /* lets change the value of a and b */ a = 0; b = 10; if ( a && b ) { printf("Line 3 - Condition is true " ); } else { printf("Line 3 - Condition is not true " ); } if ( !(a && b) ) { printf("Line 4 - Condition is true " ); } } Functions:-
#include <stdio.h> /* function declaration */ int max(int num1, int num2); int main () { /* local variable definition */ int a = 100; int b = 200; int ret; /* calling a function to get max value */ ret = max(a, b); printf( "Max value is : %d ", ret ); return 0; } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.