Bloque 3 (Reducido A Temario de Examen)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

1.

#include <stdio.h>
#include <ctype.h>

#define ALPHABET_SIZE 26

int main() {
int letterCount[ALPHABET_SIZE] = {0};
int totalLetters = 0;
char ch;

printf("Enter text (EOF to end): ");


while ((ch = getchar()) != EOF) {
if (isalpha(ch)) {
ch = tolower(ch);
letterCount[ch - 'a']++;
totalLetters++;
}
}

printf("Letter Frequencies:\n");
for (int i = 0; i < ALPHABET_SIZE; i++) {
if (letterCount[i] > 0) {
printf("%c: %.2f%%\n", i + 'a', (double)letterCount[i] / totalLetters * 100);
}
}

return 0;
}
2.
#include <stdio.h>

#define MAX_ROWS 10
#define MAX_COLS 10

void readMatrix(int matrix[MAX_ROWS][MAX_COLS], int *rows, int *cols) {


printf("Enter the number of rows and columns: ");
scanf("%d %d", rows, cols);
printf("Enter matrix elements:\n");
for (int i = 0; i < *rows; i++) {
for (int j = 0; j < *cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}

void multiplyMatrices(int firstMatrix[MAX_ROWS][MAX_COLS], int firstRows, int firstCols, int


secondMatrix[MAX_ROWS][MAX_COLS], int secondRows, int secondCols, int
resultMatrix[MAX_ROWS][MAX_COLS]) {
for (int i = 0; i < firstRows; i++) {
for (int j = 0; j < secondCols; j++) {
resultMatrix[i][j] = 0;
for (int k = 0; k < firstCols; k++) {
resultMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}

void printMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int firstMatrix[MAX_ROWS][MAX_COLS], secondMatrix[MAX_ROWS][MAX_COLS],
resultMatrix[MAX_ROWS][MAX_COLS];
int firstRows, firstCols, secondRows, secondCols;

printf("Matrix 1\n");
readMatrix(firstMatrix, &firstRows, &firstCols);

printf("Matrix 2\n");
readMatrix(secondMatrix, &secondRows, &secondCols);

if (firstCols != secondRows) {
printf("Multiplication not possible.\n");
return 1;
}

multiplyMatrices(firstMatrix, firstRows, firstCols, secondMatrix, secondRows, secondCols,


resultMatrix);
printf("Result of multiplication:\n");
printMatrix(resultMatrix, firstRows, secondCols);

return 0;
}
3.
#include <stdio.h>

void performOperation(float a, float b, char operation) {


float result;
switch (operation) {
case '+':
result = a + b;
printf("%.2f + %.2f = %.2f\n", a, b, result);
break;
case '-':
result = a - b;
printf("%.2f - %.2f = %.2f\n", a, b, result);
break;
case '*':
result = a * b;
printf("%.2f * %.2f = %.2f\n", a, b, result);
break;
case '/':
if (b == 0) {
printf("Error: Division by zero!\n");
return;
}
result = a / b;
printf("%.2f / %.2f = %.2f\n", a, b, result);
break;
default:
printf("Invalid operation.\n");
}
}

int main() {
float num1, num2;
char operation, choice;

do {
printf("Enter operation (+, -, *, /) or 'q' to quit: ");
scanf(" %c", &operation); // Space before %c to skip whitespace

if (operation == 'q') break;

printf("Enter two numbers: ");


scanf("%f %f", &num1, &num2);

performOperation(num1, num2, operation);

printf("Do another operation (y/n)? ");


scanf(" %c", &choice);
} while (choice == 'y');

return 0;
}
5.
#include <stdio.h>
#include <string.h>

#define MAX_TEXT_SIZE 1000


#define MAX_PATTERN_SIZE 100

void replacePattern(char *str, const char *pattern, const char *replacement) {


char buffer[MAX_TEXT_SIZE] = {0};
char *insert_point = &buffer[0];
const char *tmp = str;
size_t pat_len = strlen(pattern), repl_len = strlen(replacement);

while (1) {
const char *p = strstr(tmp, pattern);

// No more occurrences found


if (p == NULL) {
strcpy(insert_point, tmp);
break;
}

// Copy part before the pattern


memcpy(insert_point, tmp, p - tmp);
insert_point += p - tmp;

// Copy replacement
memcpy(insert_point, replacement, repl_len);
insert_point += repl_len;

// Adjust tmp past the pattern


tmp = p + pat_len;
}

// Copy modified string back into original string


strcpy(str, buffer);
}

int main() {
char text[MAX_TEXT_SIZE], pattern[MAX_PATTERN_SIZE],
replacement[MAX_PATTERN_SIZE];

printf("Enter text: ");


fgets(text, MAX_TEXT_SIZE, stdin);
text[strcspn(text, "\n")] = 0; // Remove trailing newline

printf("Enter pattern to replace: ");


fgets(pattern, MAX_PATTERN_SIZE, stdin);
pattern[strcspn(pattern, "\n")] = 0; // Remove trailing newline

printf("Enter replacement pattern: ");


fgets(replacement, MAX_PATTERN_SIZE, stdin);
replacement[strcspn(replacement, "\n")] = 0; // Remove trailing newline

replacePattern(text, pattern, replacement);

printf("Modified text: %s\n", text);

return 0;
}
6.
#include <stdio.h>

int calculateParity(int num) {


int parity = 0;
while (num) {
parity = !parity;
num = num & (num - 1); // Remove the lowest set bit
}
return parity;
}

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

int parity = calculateParity(num);


printf("Parity bit to make the total number of set bits even: %d\n", parity);

return 0;
}
7.
#include <stdio.h>

void bubbleSort(int arr[], int n) {


int i, j, temp;
int swapped;
do {
swapped = 0;
for (i = 1; i < n; i++) {
if (arr[i - 1] > arr[i]) {
temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
swapped = 1;
}
}
} while (swapped);
}

int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

bubbleSort(arr, n);

printf("Sorted list in ascending order:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
8.
#include <stdio.h>
#include <string.h>

#define MAX_WORDS 100


#define MAX_WORD_LENGTH 50

void sortWords(char words[][MAX_WORD_LENGTH], int count) {


char temp[MAX_WORD_LENGTH];
for (int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (strcmp(words[i], words[j]) > 0) {
strcpy(temp, words[i]);
strcpy(words[i], words[j]);
strcpy(words[j], temp);
}
}
}
}

int main() {
char words[MAX_WORDS][MAX_WORD_LENGTH];
int n;

printf("Enter number of words: ");


scanf("%d", &n);
getchar(); // consume newline

printf("Enter %d words:\n", n);


for (int i = 0; i < n; i++) {
fgets(words[i], MAX_WORD_LENGTH, stdin);
words[i][strcspn(words[i], "\n")] = 0; // remove newline
}

sortWords(words, n);

printf("Sorted list of words:\n");


for (int i = 0; i < n; i++) {
printf("%s\n", words[i]);
}

return 0;
}
9.
#include <stdio.h>
#include <string.h>

#define MAX_LINE_LENGTH 1000

int main() {
char pattern[MAX_LINE_LENGTH], line[MAX_LINE_LENGTH];

printf("Enter pattern: ");


if (!fgets(pattern, MAX_LINE_LENGTH, stdin)) {
printf("Error reading pattern.\n");
return 1;
}
pattern[strcspn(pattern, "\n")] = 0; // Remove newline

printf("Enter text (EOF to finish):\n");


while (fgets(line, MAX_LINE_LENGTH, stdin)) {
line[strcspn(line, "\n")] = 0; // Remove newline
if (strstr(line, pattern) != NULL) {
printf("%s\n", line);
}
}

return 0;
}

You might also like