Você está na página 1de 6

Ejercicio 2-1.

Escriba un programa para determinar los


if (c == '\n')
rangos de variables char,
break;
short, int y long , tanto signed como unsigned ,
}
imprimiendo los valores apropiados de los headers
line[i] = '\0';
estndar y por clculo directo . Es ms difcil si los
calcula: determine los rangos d e los varios tipos de punto printf("%s\n", line);
flotante.
}
#include<stdio.h>
main(){
unsigned char w = ~0;
printf("%u\n", w);
printf("%d\n", w/2);
printf("%d\n", -w/2 -1);
unsigned int x = ~0;
printf("%u\n", x);
printf("%d\n", x/2);
printf("%d\n", -(x/2) -1);
unsigned short int y = ~0;
printf("%u\n", y);
printf("%d\n", y/2);
printf("%d\n", -(y/2) -1);
unsigned long int z = ~0;
printf("%lu\n", z);
printf("%ld\n", z/2);
printf("%ld\n", -(z/2) -1);
}

Input :
Hola Mundo

Output :
255
127
-128
4294967295
2147483647
-2147483648
65535
32767
-32768
18446744073709551615
9223372036854775807
-9223372036854775808

main() {
int len;
char line[MAXLINE];

Output :
Hola Mundo
Ejercicio 2-3. Escriba la funcin htoi(s), que convierte
una cadena de dgitos hexadecimales (incluyendo 0x 0X
en forma optativa) en su valor en tero equivalente. Los
dgitos permitidos son del 0 al 9, de la a a la f, y de la A a
la F.

#include <stdio.h>
#define MAXLINE 1000
int get_line(char line[], int maxline);
unsigned long htoi(char s[]);

while ((len = get_line(line, MAXLINE)) > 0)


printf("%lu\n", htoi(line));
}
int get_line(char s[], int lim)
{
int c, i, l;
for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i)
if (i < lim - 1)
s[l++] = c;
// if (c == '\n' && l < lim - 1)
//
s[l++] = c;
s[l] = '\0';

Ejercicio 2-1. Escribir un bucle equivalente al bucle "for"


arriba sin usar && o ||
arriba.
#include <stdio.h>
#define MAXLINE 1000
main(){
int c, i;
char line[MAXLINE];
i = 0;
while (i < MAXLINE - 1) {
if ((c = getchar()) == EOF)
break;
line[i++] = c;

return l;
}
unsigned long htoi(char s[])
{
unsigned long n;
int i, c;
n = 0;
for (i = 0; s[i] != '\0'; ++i) {
c = s[i];

if (i == 0 && c == '0' && (s[1] == 'x' || s[1] == 'X')) { {


i = 1;
int c, i, l;
continue;
}
for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i)
n *= 16;
if (i < lim - 1)
if (c >= '0' && c <= '9')
s[l++] = c;
n += c - '0';
s[l] = '\0';
else if (c >= 'a' && c <= 'f')
n += c - 'a' + 10;
return l;
else if (c >= 'A' && c <= 'F')
}
n += c - 'A' + 10;
}
void squeeze(char s1[], char s2[])
return n;
{
}
int i, j, k;
Input :
0xFFFF
i = 0;
0xf
while (s2[i] != '\0') {
0x10
j = 0;
while (s1[j] != '\0') {
if (s1[j] == s2[i]) {
Output :
k = j;
65535
while ((s1[k] = s1[++k]) != '\0')
15
;
16
} else
++j;
Ejercicio 2-4. Escriba un a versin alterna desqueeze( s
}
l,s 2 ) que borre cada carcter de si que coincida con
++i;
cualquier carcter de la cadena s2.
}
}
Input :
#include <stdio.h>
Epis
Unsa
#define MAXLINE 1000
Output :
int get_line(char line[], int maxline);
Epi
void squeeze(char s1[], char s2[]);
Ejercicio 2-5. Escriba la funcin a n y ( s l,s 2 ) , que
int main(void)
regresa la pimera posicin de la cadena si en donde se
{
encuentre cualquier carcter de la cadena s2, o - 1 si si
int len;
no contiene caracteres d e s2. (La funcin de biblioteca
char s1[MAXLINE];
estndar strp brk hace
char s2[MAXLINE];
el mism o trabajo pero regresa una puntador a la po
sicin encontrada .)
printf(" s1:\n");
while ((len = get_line(s1, MAXLINE)) == 0)
#include <stdio.h>
;
#define MAXLINE 1000
printf(" s2:\n");
while ((len = get_line(s2, MAXLINE)) == 0)
int get_line(char line[], int maxline);
;
int any(char s1[], char s2[]);
squeeze(s1, s2);
printf("Result is %s\n", s1);

return 0;

int get_line(char s[], int lim)

int main(void)
{
int len;
char s1[MAXLINE];
char s2[MAXLINE];
printf("Input string s1:\n");

while ((len = get_line(s1, MAXLINE)) == 0)


;
printf("Input string s2:\n");
while ((len = get_line(s2, MAXLINE)) == 0)
;
printf("The first location is %d\n", any(s1, s2));
return 0;

int main(void)
{
printf("%u\n", setbits(5732, 6, 3, 9823));
return 0;
}
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
// xxxxx00000xxxxx
unsigned a = x & ~(~(~0U << n) << (p + 1 - n));

// 00000yyyyy00000
unsigned b = (y & ~(~0U << n)) << (p + 1 - n);

int get_line(char s[], int lim)


{
int c, i, l;

// xxxxxyyyyyxxxxx
return a | b;

for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) }


if (i < lim - 1)
s[l++] = c;
Output :
s[l] = '\0';
5748
return l;
}
int any(char s1[], char s2[])
{
int i, j;
i = 0;
while (s2[i] != '\0') {
j = 0;
while (s1[j] != '\0') {
if (s1[j] == s2[i])
return j;
++j;
}
++i;
}
return -1;
}

Input :
Hola
Output :
Mundo

Ejercicio 2-7. Escriba una funcin invert(x ,p ,n ) que


regresa x con los n bits que principian en la posicin p
invertidos (esto es, 1 cambiad o a 0 y viceversa), dejan do
los otros sin cambio.
#include<stdio.h>
unsigned invert(unsigned x, int p, int n);
main(){
printf("%u\n", invert(1,6,3));
}
unsigned invert(unsigned x, int p, int n){
unsigned mask = ~(~0U << n) << (p);
return mask;
}
Output :
5748
Ejercicio 2-8. Escriba una funcin rightrot(x ,n ) que
regresa el valor del entero x rotado a la derecha n
posiciones de bits.
#include <stdio.h>
unsigned rightroc(unsigned x, int n);

Ejercicio 2-6. Escriba una funcin setbits (x ,p ,n ,y ) que int main(void)


regresa x con los n bits que principian en la posicin p
{
iguales a los n bits ms a la derecha de y, dejando los
printf("%u\n", rightroc(5732, 3));
otros bits sin cambio .
return 0;
}
#include <stdio.h>
unsigned rightroc(unsigned x, int n)
unsigned setbits(unsigned x, int p, int n, unsigned y);
{
while (n-- > 0)

if(x & 1)
x = (x >> 1) | ~(~0U >> 1);
else
x = x >> 1;
return x;

Output :
2147484364

return (c >= 'A' && c<= 'Z') ? c + 'a' - 'A' : c;


}
Output:
A -> a
B -> b

Niveles jerrquicos[editar]
Ejercicio 2-9. En un sistema de nmeros de complemen to Los niveles que componen la jerarqua de memoria
a dos, x & = (x 1) borra el bit 1 de ms a la derecha en habitualmente son:
x. Explique el porqu . Utilice esta observacin para
escribir una versin ms rpida de bitcount.
Nivel 0: Registros del microprocesador o CPU
#include <stdio.h>
Nivel 1: Memoria cach
Nivel 2: Memoria primaria (RAM)
int bitcount(unsigned x);
Nivel 3: Memorias flash
Nivel 4: Disco duro (con el mecanismo de memoria virtual)
int main(void)
Nivel 5: Cintas magnticas (consideradas las ms lentas,
{
con mayor capacidad, de acceso secuencial)
printf("%d\n", bitcount(01111));
Nivel 6: Redes (actualmente se considera un nivel ms de
return 0;
la jerarqua de memorias).
}
printf("%.2f\n", x);
int bitcount(unsigned x)
{
hellomake.c
int b;
#include <hellomake.h>
for (b = 0; x != 0; x &= (x - 1)){
b++;
}
return b;
}
Output:

int main() {
// call a function in another file
myPrintHelloMake();

return(0);

hellofunc.c
Ejercicio 2-10. Reescriba la funcin flower, que convierte #include <stdio.h>
le tras maysculas e minsculas, con una expresin
#include <hellomake.h>
condicio n al en vez de un if-e lse .
#include <stdio.h>
void myPrintHelloMake(void) {
int lower(int c);

printf("Hello makefiles!\n");

int main(void)
{
char s[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;

return;

i = 0;
while (s[i] != '\0') {
printf("%c -> %c\n", s[i], lower(s[i]));
i++;
}

return 0;

int lower(int c)

}
hellomake.h
/*
example include file
*/
void myPrintHelloMake(void);
Makefile 1
hellomake: hellomake.c hellofunc.c
gcc -o hellomake hellomake.c hellofunc.c -I.

gcc -o hellomake hellomake.c hellofunc.c -I.

LISTA.C
#include <stdio.h>
#include <stdlib.h>
int length();
struct node* BuildOneTwoThree();
struct node{
int data;
struct node *next;
};
struct node* BuildOneTwoThree() {
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
|head = malloc(sizeof(struct node));
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
// allocate 3 nodes in the heap
head->data = 1;
head->next = second; // setup first node
// note: pointer assignment rule
second->data = 2;
second->next = third; // setup second node
third->data = 3;
third->next = NULL; // setup third link
// At this point, the linked list referenced by
"head"
// matches the list in the drawing.
return head;
}
int length(struct node* head) {
struct node* current = head;
int count = 0;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
main(){
int num = length(BuildOneTwoThree());
printf("%d \n",num);
}
BASH.BH

#!/bin/bash
for i in {100..110}
do
echo $i >> t.txt
gcc z.c
./a.out < t.txt > $i".txt"
rm t.txt
echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
echo "PARA EL CASO M >> N"
gcc Mn.c
time ./a.out < $i".txt"
./a.out < $i".txt"
echo "-----------"
echo "PARA EL CASO N >> M"
gcc Nm.c
time ./a.out < $i".txt"
./a.out < $i".txt"
echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
done
#include<stdio.h>
int sumarrayrows(int m, int n, int a[][n]) {
int i, j, sum = 0;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
sum += a[i][j];
return sum;
}
main() {
int x, y;
scanf("%d %d", &x, &y);
int a [x][y];
int m, n;
for(m = 0; m < x; m++)
for(n = 0; n < y; n++)
scanf("%d", &a[m][n]);
printf("Suma: %d\n", sumarrayrows(x, y, a));
}
#include<stdio.h>
main(){
int x;
scanf("%d", &x);
printf("%d %d\n", x, x);
int cont = 1;
int n, m;
for(n = 0; n < x; n++){
for(m = 0; m < x; m++)
printf("%d ", cont++);
printf("\n");
}
}
8086: (1978, 29 K transistors). One of the first singlechip, 16-bit microprocessors. The 8088, a variant of the 8086 with an 8-bit

external bus, formed


types (including double-precision floating point), along
the heart of the original IBM personal computers. IBM with 144 new
contracted with
instructions for these formats. With these extensions,
then-tiny Microsoft to develop the MS-DOS operating compilers can use
system. The origSSE instructions, rather than x87 instructions, to
inal models came with 32,768 bytes of memory and two compile floating-point
floppy drives (no
code. Introduced the NetBurst microarchitecture, which
hard drive). Architecturally, the machines were limited could operate at
to a 655,360-byte
very high clock speeds, but at the cost of high power
address spaceaddresses were only 20 bits long
consumption.
(1,048,576 bytes address157158
able), and the operating system reserved 393,216 bytes Chapter 3 Machine-Level Representation of Programs
for its own use.
Pentium 4E: (2004, 125 M transistors). Added
In 1980, Intel introduced the 8087 floating-point
hyperthreading, a method to run
coprocessor (45 K trantwo programs simultaneously on a single processor, as
sistors) to operate alongside an 8086 or 8088 processor, well as EM64T,
executing the
Intels implementation of a 64-bit extension to IA32
floating-point instructions. The 8087 established the
developed by Adfloating-point model
vanced Micro Devices (AMD), which we refer to as x86for the x86 line, often referred to as x87.
64.
80286: (1982, 134 K transistors). Added more (and now Core 2: (2006, 291 M transistors). Returned back to a
obsolete) addressing
microarchitecture similar
modes. Formed the basis of the IBM PC-AT personal
to P6. First multi-core Intel microprocessor, where
computer, the
multiple processors are
original platform for MS Windows.
implemented on a single chip. Did not support
i386: (1985, 275 K transistors). Expanded the
hyperthreading.
architecture to 32 bits. Added the
Core i7: (2008, 781 M transistors). Incorporated both
flat addressing model used by Linux and recent versions hyperthreading and
of the Windows
multi-core, with the initial version supporting two
family of operating system. This was the first machine in executing programs
the series that
on each core and up to four cores on each chip.
could support a Unix operating system.
i486: (1989, 1.2 M transistors). Improved performance void inplace_swap(int *x, int *y) {
and integrated the
*y = *x ^ *y; /*
floating-point unit onto the processor chip but did not
*x = *x ^ *y; /*
significantly change
*y = *x ^ *y; /*
the instruction set.
}
Pentium: (1993, 3.1 M transistors). Improved
performance, but only added
minor extensions to the instruction set.
PentiumPro: (1995, 5.5 M transistors). Introduced a
radically new processor
design, internally known as the P6 microarchitecture.
Added a class of
conditional move instructions to the instruction set.
Pentium II: (1997, 7 M transistors). Continuation of the
P6 microarchitecture.
Pentium III: (1999, 8.2 M transistors). Introduced SSE,
a class of instructions
for manipulating vectors of integer or floating-point
data. Each datum can
be 1, 2, or 4 bytes, packed into vectors of 128 bits. Later
versions of this
chip went up to 24 M transistors, due to the
incorporation of the level-2
cache on chip.
Pentium 4: (2000, 42 M transistors). Extended SSE to
SSE2, adding new data

Você também pode gostar