0% found this document useful (0 votes)
34 views

Modulo 4

This document discusses programming concepts including switch statements, arrays, vectors, sorting data, and string manipulation in C language. It contains code examples and explanations of switch statements as an alternative to if/else statements, using arrays and vectors to store and manipulate data, different sorting algorithms like bubble sort, and getting user input as strings for processing. Lab exercises are provided to apply these concepts in coding problems.

Uploaded by

HUNTER Y TANK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Modulo 4

This document discusses programming concepts including switch statements, arrays, vectors, sorting data, and string manipulation in C language. It contains code examples and explanations of switch statements as an alternative to if/else statements, using arrays and vectors to store and manipulate data, different sorting algorithms like bubble sort, and getting user input as strings for processing. Lab exercises are provided to apply these concepts in coding problems.

Uploaded by

HUNTER Y TANK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Universidad Tecnológica de Querétaro

DTAI. TSU en Mecatrónica-Area de automatización

Módulo 4 - Switch, agregación de datos en matrices, punteros y los conceptos básicos


de cadenas

Lenguaje de programación
Alumno:
Alan Eduardo Ortega Herrera

Grupo:
E187

Profesor:

Yara Odeth Sainz García.


Fecha:
Septiembre 27 del 2023.
 4.1.1.2 switch - the different face of ‘if’
Código:

if(i == 1)
puts("Only one?");
else if(i == 2)
puts("I want more.");
else if(i == 3)
puts("Not bad.");
else
puts("Okay.");

 4.1.1.3 switch - the different face of ‘if’


Código:

switch(i) {
case 1: puts("Only one?"); break;
case 2: puts("I want more."); break;
case 3: puts("Not bad."); break;
case 4: puts("Okay.");
}
 4.1.1.4 switch - the different face of ‘if’
Código:

switch(i) {
case 1: puts("Only one?"); break;
case 2: puts("I want more."); break;
case 3:
case 4: puts("Okay.");
}
 4.1.1.5 switch - the different face of ‘if’
Código:

switch(i) {
case 1: puts("Only one?"); break;
case 2: puts("I want more"); break;
case 3:
case 4: puts("OK"); break;
default: puts("Don't care");
}

 4.1.1.6 LAB: Switch: Part 1

Código:

#include <stdio.h>

int main(void)
{
int mes;
scanf("%d", &mes);
switch(mes)
{
case 1: puts("January"); break;
case 2 : puts("February"); break;

case 3 : puts("March"); break;

case 4 : puts("April"); break;

case 5 : puts("May"); break;

case 6 : puts("June"); break;

case 7 : puts("July"); break;

case 8 : puts("August"); break;

case 9 : puts("September"); break;

case 10 : puts("October"); break;

case 11 : puts("November"); break;

case 12 : puts("December"); break;

default: puts("Error: no such month in my calendar.");


}
return 0;
}
 4.1.1.7 LAB: Switch: Part 2

Código:

#include <stdio.h>

int main(void)
{
int mes;
scanf("%d", &mes);

int DiasAntes = 0;

int CC = 0;
switch(mes)
{
case 12: DiasAntes += 30;

case 11: DiasAntes += 31;

case 10: DiasAntes += 30;

case 9: DiasAntes += 31;


case 8: DiasAntes += 31;

case 7: DiasAntes += 30;

case 6: DiasAntes += 31;

case 5: DiasAntes += 30;

case 4: DiasAntes += 31;

case 3: DiasAntes += 29;

case 2: DiasAntes += 31;

case 1: CC = 1; break;
default: puts("Error: no such month in my calendar.");
}
if (CC)
printf("There are %d days before the given month.\n", DiasAntes);
return 0;
}
 4.1.2.2 switch - Vectors: why do you need them?

 Código:

int numbers[5];

 4.1.2.4 switch - Vectors: why do you need them?

 Código:

int numbers[5], sum = 0, i;

for(i = 0; i < 5; i++)


sum += numbers[i];
 4.1.2.5 switch - Vectors: why do you need them?

 Código:

int i, numbers[5];

for(i = 0; i < 5; i++)


numbers[i] = 2012;

 4.1.2.6 switch - Vectors: why do you need them?

 Código:

int variable1 = 1, variable2 = 2;

variable2 = variable1;
variable1 = variable2;
 4.1.2.7 switch - Vectors: why do you need them?

 Código:

int variable1 = 1, variable2 = 2, auxiliary;

auxiliary = variable1;
variable1 = variable2;
variable2 = auxiliary;
 4.1.2.8 switch - Vectors: why do you need them?

 Código:

/* swap elements #1 and #5 */


auxiliary = numbers[0];
numbers[0] = numbers[4];
numbers[4] = auxiliary;

/* swap elements #2 and #4 */


auxiliary = numbers[1];
numbers[1] = numbers[3];
numbers[3] = auxiliary;

 4.1.2.9 switch - Vectors: why do you need them?

 Código:

for(i = 0; i < 2; i++) {


auxiliary = numbers[i];
numbers[i] = numbers[4 - i];
numbers[4 - i] = auxiliary;
}
 4.1.2.10 LAB: Arrays: Part 1

Código:

#include <stdio.h>

int main(void)
{
int Fibonacci[10], i;

Fibonacci[0] = 1;
Fibonacci[1] = 1;

for (i = 2; i<10; i++)


{
Fibonacci[i] = Fibonacci[i - 1] + Fibonacci[i - 2];
}
for (i = 0; i<10; i++)
{
printf("%d\n", Fibonacci[i]);
}
for (i = 0; i<10; i += 2)
{
printf("%d\n", Fibonacci[i]);
}
for (i = 1; i<10; i += 2)
{
printf("%d\n", Fibonacci[i]);
}
return 0;
}

 4.1.2.11 LAB: Arrays: Part 2

Código:

#include <stdio.h>

int main(void)
{
char alfabeto[26], c;

for (c = 'a'; c<='z'; c++)


{
alfabeto[c-'a'] = c;
}
for (c = 'z'; c > 'a'; c--)
{
printf("%c\n", alfabeto[c - 'a']);
}
printf("%c", alfabeto['g'-'a']);
printf("%c", alfabeto['r' - 'a']);
printf("%c", alfabeto['e' - 'a']);
printf("%c", alfabeto['a' - 'a']);
printf("%c\n", alfabeto['t' - 'a']);
return 0;
}

 4.1.3.4 Sorting data: in real life and in the computer memory

 Código:

int numbers[5]; /* array to be sorted */


int i; /* a variable for the loop */
int aux; /* auxiliary variable for swaps */

/* we need 5 – 1 comparisons – why? */


for(i = 0; i < 4; i++) {
/* compare adjacent elements */
if( numbers[i] > numbers[i + 1]) {
/* if we went here it means that we have to swap the elements */
aux = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = aux;
}

 4.1.3.4 Sorting data: in real life and in the computer memory

 Código:

/* The bubble sort - final version */

#include <stdio.h>
int main(void) {
int numbers[5];
int i, aux;
int swapped;

/* ask the user to enter 5 values */


for(i = 0; i < 5; i++) {
printf("\nEnter value #%i\n",i + 1);
scanf("%d",&numbers[i]);
}
/* sort them */
do {
swapped = 0;
for(i = 0; i < 4; i++) {
if(numbers[i] > numbers[i + 1]) {
swapped = 1;
aux = numbers[i];
numbers[i] = numbers[i + 1];
numbers[i + 1] = aux;
}
}
} while(swapped);

/* print results */
printf("\nSorted array: ");
for(i = 0; i < 5; i++)
printf("%d ",numbers[i]);

printf("\n");
return 0;
}
 4.1.3.6 LAB: Bubble sort

 Código:

#include <stdio.h>

int main(void)
{
float numbers[10] = {5.6, 4.3, 6.2, 6.4, 7.3, 2.3, 8.3, 9.2, 0.1, 1.9};
int i, intercambio;

do
{
intercambio= 0;
for (i = 0; i < 10; i++)
{
if (numbers[i] < numbers[i + 1])
{
intercambio = 1;
float number= numbers[i];

numbers[i] = numbers[i + 1];


numbers[i + 1] = number;
}
}
for (i = 0; i < 10; i++)
printf("%.2f ", numbers[i]);
printf("\n");
}

while (intercambio);

for (i = 0; i < 10; i++)


printf("%.2f ", numbers[i]);
printf("\n");
return 0;
}

 4.1.4.3 LAB: Arrays: Initiators

 Código:

#include <stdio.h>

int main(void)
{
int zeros[10] = { 0 };
int ones[] = { 1, 1 };
int superOnes[5] = { 1, 1, 1, 1, 1};
int fiboSequence[6] = { 1, 1, 2, 3, 5, 8 };
int i;
for (i = 0; i<10; i++)
{
printf("%d ", zeros[i]);
}
puts("");
for (i = 0; i<2; i++)
{
printf("%d ", ones[i]);
}
puts("");
for (i = 0; i<5; i++)
{
printf("%d ", superOnes[i]);
}
puts("");
for (i = 0; i<6; i++)
{
printf("%d ", fiboSequence[i]);
}
puts("");

return 0;
}

 4.1.4.3 LAB: Arrays: text – Part 1

 Código:

#include <stdio.h>

int main(void)
{
char nombre1[20];
char nombre2[20];
char nombre3[20];
char nombre4[20];

scanf("%s", nombre1);
scanf("%s", nombre2);
scanf("%s", nombre3);
scanf("%s", nombre4);
printf("%s\n", nombre4);
printf("%s\n", nombre3);
printf("%s\n", nombre2);
printf("%s\n", nombre1);
return 0;
}

 4.1.5.3 LAB: Arrays: text – Part 2

 Código:

#include <stdio.h>

int main(void)
{
char IPAddressParte1[4];
char IPAddressParte2[4];
char IPAddressParte3[4];
char IPAddressParte4[4];
char IPAddress[16];

scanf("%s", IPAddressParte1);
scanf("%s", IPAddressParte2);
scanf("%s", IPAddressParte3);
scanf("%s", IPAddressParte4);

sprintf(IPAddress, "%s.%s.%s.%s", IPAddressParte1, IPAddressParte2, IPAddressParte3,


IPAddressParte4);
printf("%s\n", IPAddress);
return 0;
}
 4.1.6.8 Pointers: another kind of data in the “C” language
 Código:

#include <stdio.h>

int main(void) {

printf("This computing environment uses:\n");


printf("%d byte for chars\n", sizeof(char));
printf("%d bytes for shorts\n", sizeof(short int));
printf("%d bytes for ints\n", sizeof(int));
printf("%d bytes for longs\n", sizeof(long int));
printf("%d bytes for long longs\n", sizeof(long long int));
printf("%d bytes for floats\n", sizeof(float));
printf("%d bytes for doubles\n", sizeof(double));
printf("%d bytes for pointers\n", sizeof(int *));
return 0;
}
 4.1.6.9 LAB: Pointers and sizeof

 Código:

#include <stdio.h>

int main(void)
{
int v;
printf("%d byte for chars\n", sizeof(char));
char c;
printf("%d byte for char variables\n", sizeof(c));

printf("%d bytes for shorts\n", sizeof(short int));


short int si;
printf("%d bytes for short variables\n", sizeof(si));

printf("%d bytes for ints\n", sizeof(int));


int i;
printf("%d bytes for int variables\n", sizeof(i));

printf("%d bytes for longs\n", sizeof(long int));


long int li;
printf("%d bytes for long variables\n", sizeof(li));

printf("%d bytes for long longs\n", sizeof(long long int));


long long int lli;
printf("%d bytes for long long variables\n", sizeof(lli));

printf("%d bytes for floats\n", sizeof(float));


float f;
printf("%d bytes for float variables\n", sizeof(f));

printf("%d bytes for doubles\n", sizeof(double));


double d;
printf("%d bytes for double variables\n", sizeof(d));

printf("%d bytes for pointers\n", sizeof(int *));


int *pi;
printf("%d bytes for pointer variables\n", sizeof(pi));

printf("%d bytes for address of char variable\n", sizeof(&c));


char *pc=&c;
printf("%d bytes for pointer to char variable\n", sizeof(pc));
printf("%d byte for value stored by pointer to char variable\n", sizeof(*pc));

return 0;
}

 4.1.7.2 Pointers: another kind of data in the “C” language


 Código:

int *ptr1, *ptr2, array[3], i;

ptr1 = array;
 4.1.7.6 LAB: Pointers: advanced
 Código:

#include <stdio.h>

int main(void)
{
int i;
float numbers[10] = {3.3, 4.4, 5.2, 2.3, 4.5, 6.8, 9.8, 8.2, 7.5, 9.2};
float biggerNumbers[10];
float *source = numbers;
float *copy = biggerNumbers;
for (i = 0; i < 10; i++)
{
*copy = *source;
copy++;
source++;
}
float *biggerEnd = copy;
float *biggerStart = biggerNumbers;
float *middle1 = biggerNumbers+4;
float *middle2 = middle1+1;
for ( ; middle1>=biggerStart ; middle1--, middle2++)
{
printf("%.1f\n", *middle1);
printf("%.1f\n", *middle2);
}
return 0;
}

 4.1.9.1 Assigning values to strings


 Código:

char protagonist[] = "Snape";


 4.1.9.2 y 4.1.9.3 Assigning values to strings
 Código:

char protagonist[20];
protagonist = "Gandalf";

 4.1.9.4 Assigning values to strings


 Código:

char protagonist[10] = "Frodo";


 4.1.9.5 Assigning values to strings
 Código:

char *hero = "Dumbledore";

 4.1.9.6 Assigning values to strings


 Código:

hero = "Sirius";
 4.1.9.7 Assigning values to strings
 Código:

strcpy(hero, "Pippin");

 4.1.9.8 LAB: Strings


 Código:

#include <stdio.h>
#include <string.h>

int main(void)
{
int i;

char IPAddress[20];
char IPAddressL3[20];
char IPAddressL2[10];
char IPAddressL1[5];

int part3Count = 0;
int part2Count = 0;
int part1Count = 0;
int partsCount = 0;
int actualCount = 0;

scanf("%s", IPAddress);
int isError = 0;
for (i=0 ; i<strlen(IPAddress) ; i++)
{
char c = IPAddress[i];
if (c >= '0' && c <= '9' || c == '.')
{
if (c=='.')
{
if(actualCount>3)
{
isError = 1;
break;
}
partsCount++;
if (partsCount>3)
{
isError = 1;
break;
}
if (partsCount == 2)
{
IPAddressL3[part3Count] = c;
part3Count++;
}
if (partsCount == 3)
{
IPAddressL3[part3Count] = c;
part3Count++;
IPAddressL2[part2Count] = c;
part2Count++;
}
actualCount = 0;
}
else
{
if(partsCount==1)
{
IPAddressL3[part3Count] = c;
part3Count++;
}
if (partsCount == 2)
{
IPAddressL3[part3Count] = c;
part3Count++;
IPAddressL2[part2Count] = c;
part2Count++;
}
if (partsCount == 3)
{
IPAddressL3[part3Count] = c;
part3Count++;
IPAddressL2[part2Count] = c;
part2Count++;
IPAddressL1[part1Count] = c;
part1Count++;
}
actualCount++;
}

}
else
{
isError = 1;
break;
}
}
if (isError)
puts("Error: not a valid address.");
else
{
IPAddressL3[part3Count] = '\0';
IPAddressL2[part2Count] = '\0';
IPAddressL1[part1Count] = '\0';
printf("Last 3 parts: %s\n", IPAddressL3);
printf("Last 2 parts: %s\n", IPAddressL2);
printf("Last 1 part: %s\n", IPAddressL1);
}
return 0;
}

 4.1.10.2 Processing strings


 Código:

int res;

res = puts("McGonagall");
puts("Saruman");
 4.1.10.3 Processing strings
 Código:

int printf(char *format, ...);

 4.1.10.4 Processing strings


 Código:

printf("%s and %s are the best\n", protagonist, hero);


 4.1.10.6 Processing strings
 Código:

int strlen(char *s);

 4.1.10.7 Processing strings


 Código:

char *strcpy(char *destination, char *source);


 4.1.10.8 Processing strings
 Código:

char *strncpy(char *destination, char *source, int n);

 4.1.10.9 Processing strings


 Código:

char *strcat(char *destination, char *source);


 4.1.10.10 LAB: Strings: advanced operations
 Código:

#include <stdio.h>
#include <string.h>

int main(void)
{
char word[256];
int i;
scanf("%s", word);
int length = strlen(word);
for(i=0 ; i < length/2 ; i++)
{
char c = word[i];
word[i] = word[length - i - 1];
word[length - i - 1] = c;
}
printf("%s\n", word);
return 0;
}

You might also like