Está en la página 1de 14

WORKSHOP 1 CUT 2 IMPERATIVE PROGRAMMING

Student : Nicolas Esteban Beltrán Ramos


Code : 67001076
Mail : nebeltran76@ucatolica.edu.co
Bogotá D.C
17/04/2023
Introduction
In this work we will show the development of workshop #1 of cut #2. The workshop consists of
8 points which deal with the resolution of application of problems in the C++ programming
language, in this case the functions, matrices and vectors.
Keywords: (C++, workshop, the functions, matrices , vectors).
Objectives
General Objective
- Correctly and completely develop the resolution of workshop 1 of cut 2.
Specific Objectives
- Set out the input, output and conditions of each application problem.
- Create the respective code for each application problem.
- To show failures in the code
Exercice 1

#include <iostream>
using namespace std;

int main() {
const int S = 101;
int cont[S] = {0};
int i;

cout << "Enter integers between 1 and 100 :" << endl;
cin >> i;
while (i != 0) {
if (i >= 1 && i <= 100) {
cont[i]++;
}
cin >> i;
}

for (int w = 1; w <= 100; w++) {


if (cont[w] > 0) {
cout << w << " occurs " << cont[w] << " time(s)" << endl;
}
}

return 0;
}
Exercise 2

#include <iostream>

using namespace std;

int main() {
int t = 10; // Tamaño del arreglo
int numeros[t]; // Arreglo para almacenar los números distintos
int cont = 0; // Contador de números distintos
int num;

// Leemos los números y los almacenamos si son distintos


for (int i = 0; i < t; i++) {
cout << "Ingrese un numero: ";
cin >> num;
bool e = false;
for (int j = 0; j < cont; j++) {
if (num == numeros[j]) {
e = true;
break;
}
}
if (!e) {
numeros[cont] = num;
cont++;
}
}

// Mostramos los números distintos


cout << "Los números distintos son:";
for (int i = 0; i < cont; i++) {
cout << " " << numeros[i];
}
cout << endl;

return 0;
}
Exercice 3

#include <iostream>
#include <algorithm>
//ejer 3
using namespace std;

int* eliminarDuplicados(int arr[], int n) {


sort(arr, arr+n); // Ordenar el arreglo para poder eliminar los duplicados
int* nuevaMatriz = new int[n]; // Crear una nueva matriz para almacenar los
elementos únicos
int j = 0;
for (int i = 0; i < n; i++) {
if (i == 0 || arr[i] != arr[i-1]) { // Si el elemento actual es único
nuevaMatriz[j++] = arr[i]; // Agregarlo a la nueva matriz
}
}
return nuevaMatriz;
}

int main() {
const int TAM = 10;
int matriz[TAM];

// Leer la matriz del usuario


for (int i = 0; i < TAM; i++) {
cout << "Ingrese el elemento " << i+1 << ": ";
cin >> matriz[i];
}

// Eliminar los duplicados de la matriz


int* nuevaMatriz = eliminarDuplicados(matriz, TAM);
// Mostrar la nueva matriz sin duplicados
cout << "La nueva matriz sin duplicados es: ";
for (int i = 0; i < TAM && nuevaMatriz[i] != 0; i++) {
cout << nuevaMatriz[i] << " ";
}
cout << endl;

// Liberar la memoria de la nueva matriz


delete[] nuevaMatriz;

return 0;
}

Exercice 4

#include <iostream>
using namespace std;

const int f = 3;
const int c = 4;

double sumacolumna(double m[][c], int columnin) {


double sum = 0.0;
for (int i = 0; i < f; i++) {
sum += m[i][columnin];
}
return sum;
}
int main() {
double mat[f][c];
for (int i = 0; i < f; i++) {
for (int j = 0; j < c; j++) {
cout << "Ingrese el valor de la fila " << i+1 << " y columna " <<
j+1 << ": ";
cin >> mat[i][j];
}
}
cout << endl;

for (int j = 0; j < c; j++) {


double sum = sumacolumna(mat, j);
cout << "la suma de la columna " << j+1 << " es " << sum << endl;
}

return 0;
}

Exercice 5
#include <iostream>
#include <algorithm>
using namespace std;
//ejer 5

const int NUM_EMPLEADOS = 8;


const int NUM_DIAS = 7;

int main() {
int horas_semanales[NUM_EMPLEADOS][NUM_DIAS] = {
{8, 7, 8, 6, 8, 5, 7},
{6, 6, 7, 5, 6, 6, 5},
{8, 8, 8, 8, 8, 8, 8},
{4, 4, 4, 4, 4, 4, 4},
{10, 10, 10, 10, 10, 10, 10},
{3, 3, 3, 3, 3, 3, 3},
{9, 9, 9, 9, 9, 9, 9},
{7, 7, 7, 7, 7, 7, 7}
};

int totales_horas[NUM_EMPLEADOS];
for (int i = 0; i < NUM_EMPLEADOS; i++) {
int total = 0;
for (int j = 0; j < NUM_DIAS; j++) {
total += horas_semanales[i][j];
}
totales_horas[i] = total;
}

// Crear una lista de empleados y sus totales de horas


pair<int, int> empleados[NUM_EMPLEADOS];
for (int i = 0; i < NUM_EMPLEADOS; i++) {
empleados[i] = make_pair(i+1, totales_horas[i]); // i+1 porque los
empleados empiezan en 1, no en 0
}

// Ordenar la lista de empleados y sus totales de horas en orden decreciente


sort(empleados, empleados+NUM_EMPLEADOS, [](pair<int, int> a, pair<int,
int> b) {
return a.second > b.second;
});
// Mostrar los empleados y sus totales de horas en orden decreciente
cout << "Empleados y sus totales de horas:" << endl;
for (int i = 0; i < NUM_EMPLEADOS; i++) {
cout << "Empleado " << empleados[i].first << ": " <<
empleados[i].second << " horas" << endl;
}

return 0;
}

Exercice 6

#include <iostream>
using namespace std;
//ejer 6
const int ROWS = 3;
const int COLS = 3;

void leerMatriz(double matriz[][COLS], int filas, int columnas) {


for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
cout << "Ingrese el elemento [" << i << "][" << j << "]: ";
cin >> matriz[i][j];
}
}
}

void sumarMatrices(double matriz1[][COLS], double matriz2[][COLS], double


matriz_suma[][COLS], int filas, int columnas) {
for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
matriz_suma[i][j] = matriz1[i][j] + matriz2[i][j];
}
}
}

void mostrarMatriz(double matriz[][COLS], int filas, int columnas) {


for (int i = 0; i < filas; i++) {
for (int j = 0; j < columnas; j++) {
cout << matriz[i][j] << " ";
}
cout << endl;
}
}

int main() {
double matriz1[ROWS][COLS];
double matriz2[ROWS][COLS];
double matriz_suma[ROWS][COLS];

// Leer las dos matrices del usuario


cout << "Ingrese la primera matriz:" << endl;
leerMatriz(matriz1, ROWS, COLS);

cout << "Ingrese la segunda matriz:" << endl;


leerMatriz(matriz2, ROWS, COLS);

// Sumar las dos matrices y mostrar el resultado


sumarMatrices(matriz1, matriz2, matriz_suma, ROWS, COLS);
cout << "La suma de las dos matrices es:" << endl;
mostrarMatriz(matriz_suma, ROWS, COLS);
return 0;
}

Exercice 7

#include <iostream>
using namespace std;
//ejer 7
const int ROWS = 3;
const int COLS = 3;

int main() {
double matriz1[ROWS][COLS], matriz2[ROWS][COLS], producto[ROWS][COLS]
= {0};

// Leer las dos matrices del usuario


cout << "Ingrese la primera matriz:" << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cout << "Ingrese el elemento [" << i << "][" << j << "]: ";
cin >> matriz1[i][j];
}
}
cout << "Ingrese la segunda matriz:" << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cout << "Ingrese el elemento [" << i << "][" << j << "]: ";
cin >> matriz2[i][j];
}
}

// Multiplicar las dos matrices


for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
for (int k = 0; k < COLS; k++) {
producto[i][j] += matriz1[i][k] * matriz2[k][j];
}
}
}

// Mostrar el producto
cout << "El producto de las dos matrices es:" << endl;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cout << producto[i][j] << " ";
}
cout << endl;
}

return 0;
}

Exercice 8
#include <iostream>
using namespace std;

int* encontrarM(int** m, int filas, int columnas) {


int* max = new int[2];
max[0] = 0;
max[1] = 0;
int valorMax = m[0][0];
for (int fila = 0; fila < filas; fila++) {
for (int columna = 0; columna < columnas; columna++) {
if (m[fila][columna] > valorMax) {
valorMax = m[fila][columna];
max[0] = fila;
max[1] = columna;
}
}
}
return max;
}

int main() {
int filas, columnas;
cout << "Ingrese el numero de filas: ";
cin >> filas;
cout << "Ingrese el numero de columnas: ";
cin >> columnas;

// Crear matriz
int** m = new int*[filas];
for (int fila = 0; fila < filas; fila++) {
m[fila] = new int[columnas];
for (int columna = 0; columna < columnas; columna++) {
cout << "Ingrese el elemento (" << fila << ", " << columna << "): ";
cin >> m[fila][columna];
}
}

// Encontrar el elemento máximo


int* max = encontrarM(m, filas, columnas);
cout << "El elemento máximo se encuentra en la posicion (" << max[0] << ", " <<
max[1] << ")" << endl;

// Liberar memoria
for (int fila = 0; fila < filas; fila++) {
delete[] m[fila];
}
delete[] m;
delete[] max;

return 0;
}

Conclusions
functions are fundamental to code development, as are vectors and matrices, since they
can be used as the tools they are for solving most problems.

También podría gustarte