Modulo 5
Modulo 5
Lenguaje de programación
Alumno:
Alan Eduardo Ortega Herrera
Grupo:
E187
Profesor:
#include <stdio.h>
#include <string.h>
int main(void) {
char *ptr;
strcpy(ptr, "you may get into trouble soon");
puts(ptr);
return 0;
}
#include <stdio.h>
int main(void) {
char *ptr;
*ptr = 'C';
printf("%c",*ptr);
return 0;
}
5.1.2.3 Using pointers: perils and disadvantages
Código:
#include <stdio.h>
#include <string.h>
int main(void) {
char str[10];
strcpy(str,"Welcome to Troubleland!");
printf("%s",str);
return 0;
}
5.1.2.4 Using pointers: perils and disadvantages
Código:
#include <stdio.h>
#include <string.h>
int main(void) {
char str[10];
int i;
strcat(str,"Bump!");
printf("%s",str);
return 0;
}
float temp[31][24];
int day;
float sum = 0.0, average;
float temp[31][24];
int day,hour;
float max = -100.0;
float temp[31][24];
int day,hour;
int hotdays = 0;
float temp[31][24];
int d,h;
#include <stdio.h>
int main(void)
{
char board[8][8];
int i;
board[0][0] = 'R';
board[0][1] = 'N';
board[0][2] = 'B';
board[0][3] = 'Q';
board[0][4] = 'K';
board[0][5] = 'B';
board[0][6] = 'N';
board[0][7] = 'R';
board[7][0] = 'R';
board[7][1] = 'N';
board[7][2] = 'B';
board[7][3] = 'Q';
board[7][4] = 'K';
board[7][5] = 'B';
board[7][6] = 'N';
board[7][7] = 'R';
#include <stdio.h>
int main(void)
{
char days[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday"};
int day;
scanf("%d", &day);
if (day >= 0 && day < 7)
{
printf("Pointer version: %s\n", days + day);
printf("Array index version: %s\n", days[day]);
}
else
puts("Error, no such day.");
return 0;
}
5.1.4.4 Memory allocation and deallocation: malloc() and free()
Código:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *ptr;
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *numbers, how_many_numbers;
int i, aux;
int swapped;
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int size;
scanf("%d", &size);
if(size>=1024*1024)
{
puts("Too much memory requested.");
}
else
{
char *values=(char *) malloc (sizeof(char) * size) ;
int i;
for(i=0 ; i < size ; i++)
{
values[i] = 'A' + (i % 26);
}
int boundaries = size;
if (boundaries > 400)
boundaries = 400;
for (i=0 ; i < boundaries ; i++)
{
printf("%c", values[i]);
if ((i+1) % 40 == 0)
printf("\n");
}
printf("\n");
}
return 0;
}
int **ptrtab;
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int size;
scanf("%d", &size);
if(size>20)
{
puts("Matrix too big.");
}
else
{
int **matrix = (int**)malloc(sizeof(int *) * size);
int i, j;
for (i=0; i < size ; i++)
{
matrix[i] = (int*)malloc(sizeof(int) * size);
}
}
return 0;
}
#include <stdio.h>
void hello(void);
int main(void) {
printf("We are about to invoke hello()!\n");
hello();
printf("We returned from hello()!\n");
return 0;
}
void hello(void) {
printf ("You've invoked me – what fun!\n");
return;
}
#include <stdio.h>
void hello(void) {
int i;
#include <stdio.h>
int global;
void fun(void) {
int local;
local = 2;
global++;
printf("fun: local=%d global=%d\n", local, global);
global++;
}
int main(void) {
int local;
local = 1;
global = 1;
printf("main: local=%d global=%d\n", local, global);
fun();
printf("main: local=%d global=%d\n", local, global);
return 0;
}
int notmany = 5;
hello2(100); /* the actual parameter is a literal */
hello2(notmany); /* the actual parameter is a variable */
hello2(2 * notmany); /* the actual parameter is an expression */
#include <math.h>
#include <stdio.h>
int main(void) {
float a, b, a_sqr, b_sqr, c;
printf("A?\n");
scanf("%f", &a);
a_sqr = a * a;
printf("B?\n");
scanf("%f", &b);
b_sqr = b * b;
c = sqrt(a_sqr + b_sqr);
printf("The length of the hypotenuse is: %f\n", c);
return 0;
}
#include <math.h>
#include <stdio.h>
int main(void) {
float a, b, a_sqr, b_sqr, c;
printf("A?\n");
scanf("%f", &a);
a_sqr = square(a);
printf("B?\n");
scanf("%f", &b);
b_sqr = square(b);
c = sqrt(a_sqr + b_sqr);
printf("The length of the hypotenuse is: %f\n", c);
return 0;
}
#include <stdio.h>
void hello()
{
puts("Hello!");
}
void another()
{
puts("It's me - another function.");
}
int main(void)
{
hello();
hello();
hello();
another();
hello();
another();
return 0;
}
#include <stdio.h>
int main(void)
{
int fiveValue = getValue(1, 5.6);
int sixValue = getValue(11, 5.6);
int sevenValue = getValue(11, 5.6) + getOneOrTwo(0);
int eightValue = getValue(11, 5.6) + getOneOrTwo(6);
int nineValue = getValue(11, 5.6) + getOneOrTwo(0) + getOneOrTwo(6);
#include <stdio.h>
int main(void)
{
double tenValue = getMaxOfThree(5, 9, 10);
double bigValue = getMaxOfThree(555.4, 555.3, 556.4);
printf("Ten: %.2f\n", tenValue);
printf("Big value: %.2f\n", bigValue);
return 0;
}
5.1.9.15 LAB: Functions: Part 4 - Function call
Código:
#include <stdio.h>
int main(void)
{
double twentyFiveValue = power(5.0, 2);
double piSquaredValue = power(3.14159265, 2);
double piCubedValue = power(3.14159265, 3);
double bigPower = power(1.23, 20);
double millionValue = power(10, 6);
#include <stdio.h>
int main(void)
{
int thirtyFiveValue = getValue(4, 2.4);
int thirtyValue1 = getValue(4, 2.6);
int thirtyValue2 = getValue(6, 2.4);
int twentyValue = getValue(6, 2.6);
int twoValue = getExclusive(2, 1);
int zeroValue = getExclusive(2, 2);
#include <stdio.h>
int main(void)
{
int result1 = stringCompare("AAA", "BBB");
int result2 = stringCompare("AAC", "AAB");
int result3 = stringCompare("AAC", "AAC");
int result4 = stringCompare("AAC", "AACC");