[go: up one dir, main page]

0% found this document useful (0 votes)
8 views5 pages

Seal

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

CODE 0202

NAME OMASET GARD


REG NO SCT 121-0540/2023
TITLE INTRODUCTION TO PROGRAMMING AND ALGORITHMS.
CAT 2 Introduction to Programming
1. Discuss the term pointer as used in C Programming.
Pointers are features of C programming language that allow us to store
addresses or memory locations rather than values.
2.Write a C program that adds three numbers by pointing to these numbers and
outputting their sum by dereferencing the pointers.

/*#include <stdio.h>// header files for starndad library functions


int main() // the main function
{
int num1, num2 , num3; // we declare and initialize the three integers
int* ptr1, * ptr2, * ptr3; // declaration of the pointers
printf("Enter the three numbers:"); // this is from the user to input.
scanf_s("%d %d %d",&num1,&num2,&num3);//we store our values
//we assign the address of the numbers to their pointers.
ptr1 = &num1;
ptr2 = &num2;
ptr3 = &num3;
int sum = *ptr1 + *ptr2 + *ptr3; //we add the numbers by dereferencing the
pointers.
//the asteric symbolises the dereferencer operator.
printf("The sum is :%d\n",sum);//we print the sum of the numbers
return 0;
}

References – class notes


-Programiz.com. 2
3.Discuss 5 convections used in C Programming
1 Naming Conventions:
Meaningful Names: Choose descriptive names for variables, functions, and other
identifiers. Clear names improve code readability.
Consistent Style: Stick to a consistent naming convention style throughout your
codebase/ codeblock (e.g., camelCase for naming functions).
Avoid Abbreviations: Use full words instead of abbreviations for better clarity.
Indentation and Braces:
Consistent Indentation: Properly indent code blocks (e.g., loops, conditionals,
functions) this improves readability of the codeblocks.
Brace Placement: Place opening braces { on the same line as the function or control
statement, and closing braces } on a new line.
Comments:
Explain Complex Sections of your code: Use comments to explain unique or non-
obvious parts of your code.
Document Functions: Describe the purpose, parameters, and return values of
functions using comments.
Constants:
Use #define or const: Define constants using #define or const to improve code
maintainability.
Meaningful Names: Give meaningful names to constants (e.g., MAX_SIZE, PI).
Function Declarations and Definitions:
Function Naming: Use descriptive names for functions (e.g., firstName or
lastName).
Separate Declarations and Definitions: Declare functions in header files and define
them in source files. This separation enhances modularity.

References – class notes


-Programiz.com. 2
Assignment 1:
1. Using C, make a program that given the array int numbers = {45, 29, 11, 89, 42,
100, 34, 22, 32}, it can sort them from highest to lowest.

1#include <stdio.h>
2. void sortDescending(int arr[], int n) {
3. int i, g, max_index, temp;
4.
5. // One after the other move the boundary of the unsorted subarray
6. for (i = 0; i < n - 1; i++) {
7. // Find the maximum element in the given unsorted array
8. max_index = i;
9. for (g = i + 1; g < n; g++) {
10. if (arr[g] > arr[max_index]) {
11. max_index = g;
12. }
13. }
14.
15. // Swap the found maximum element with the first element
16. temp = arr[max_index];
17. arr[max_index] = arr[i];
18. arr[i] = temp;
19. }
20. }
21. int main() {
22. int numbers[] = { 45, 29, 11, 89, 42, 100, 34, 22, 32 };
23. int n = sizeof(numbers) / sizeof(numbers[0]);
24.
25. sortDescending(numbers, n);
26.
27. printf("Sorted array from highest to lowest: \n");
28. for (int i = 0; i < n; i++) {
29. printf("%d ", numbers[i]);
30. }
31. printf("\n");
32.
33. return 0;
34. }

References – class notes


-Programiz.com. 2
2Write a C program that generates a random number between 1 and 100 and
asks the user to guess it. Use a do-while loop to give the user multiple
chances until they guess the correct number.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
int randomNumber,Guess,attempts = 0;
srand(time(0));
randomNumber = rand() % 100 + 1;
printf("Try and guess the number between 1 and 100\n");
do {
printf("Enter your guess");
scanf_s("%d", &Guess);
attempts++;
if (Guess>randomNumber)
{
printf("The number is too large! Pick another number.\n");

}
else if (Guess<randomNumber){
printf("Its too low!! Try again.\n");

}
else {
printf("Wow you made the right guess in %d attempts.\n",attempts);
}

} while ("guess!=randomNumber");
return 0;
}

References – class notes


-Programiz.com. 2
3.Write a C program to convert a given any integer to a Roman number. >

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.


Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Example Expected Output:
Original integer: 12
Roman number of the said integer: XII

#include<stdio.h>
#include<string.h>
int main()
{
char romanSymbols[] = { 'M','D','C','L','X','V','I' };
int romanValues[] = { 1000,500,100,50,10,5,1 };
int number;
char romanNumeral[20];
printf("Enter an integer value:");
scanf_s("%d", &number);
int i = 0;
while (number > 0); {
if (number >= romanValues[i]) {
strcat_s(romanNumeral, &romanSymbols[i]);
number -= romanValues[i];
}
else {
i++;
}
}
printf("Original integer;%d\n", number);
printf("Roman number of the integer;%s\n", romanNumeral);

return 0;
}

References – class notes


-Programiz.com. 2

You might also like