Examen de PS

download Examen de PS

If you can't read please download the document

description

PS

Transcript of Examen de PS

Ejercicio 2-1. Escriba un programa para determinar los rangos de variables char,short, int y long , tanto signed como unsigned , imprimiendo los valores apropiados de los headers estndar y por clculo directo . Es ms difcil si los calcula: determine los rangos d e los varios tipos de punto flotante.

#includemain(){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);}

Output :255127-12842949672952147483647-21474836486553532767-32768184467440737095516159223372036854775807-9223372036854775808

Ejercicio 2-1. Escribir un bucle equivalente al bucle "for" arriba sin usar && o ||arriba.#include

#define MAXLINE 1000

main(){int c, i;char line[MAXLINE];

i = 0;while (i < MAXLINE - 1) {if ((c = getchar()) == EOF)break;line[i++] = c;if (c == '\n')break;}line[i] = '\0';

printf("%s\n", line);}

Input :Hola Mundo

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

#define MAXLINE 1000

int get_line(char line[], int maxline);unsigned long htoi(char s[]);

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

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';

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;continue;}n *= 16;if (c >= '0' && c = 'a' && c = 'A' && c 1;return x;}

Output :2147484364

Ejercicio 2-9. En un sistema de nmeros de complemen to a dos, x & = (x 1) borra el bit 1 de ms a la derecha en x. Explique el porqu . Utilice esta observacin para escribir una versin ms rpida de bitcount.#include

int bitcount(unsigned x);

int main(void){printf("%d\n", bitcount(01111));return 0;}

int bitcount(unsigned x){int b;

for (b = 0; x != 0; x &= (x - 1)){b++;}return b;}Output:

Ejercicio 2-10. Reescriba la funcin flower, que convierte le tras maysculas e minsculas, con una expresin condicio n al en vez de un if-e lse .#include

int lower(int c);

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

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

return 0;}

int lower(int c){return (c >= 'A' && c aB -> b

Niveles jerrquicos[editar]Los niveles que componen la jerarqua de memoria habitualmente son:

Nivel 0: Registros del microprocesador o CPUNivel 1: Memoria cachNivel 2: Memoria primaria (RAM)Nivel 3: Memorias flashNivel 4: Disco duro (con el mecanismo de memoria virtual)Nivel 5: Cintas magnticas (consideradas las ms lentas, con mayor capacidad, de acceso secuencial)Nivel 6: Redes (actualmente se considera un nivel ms de la jerarqua de memorias).

printf("%.2f\n", x);

hellomake.c#include

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

return(0);}hellofunc.c#include #include

void myPrintHelloMake(void) {

printf("Hello makefiles!\n");

return;}hellomake.h/*example include file*/

void myPrintHelloMake(void);Makefile 1hellomake: hellomake.c hellofunc.cgcc -o hellomake hellomake.c hellofunc.c -I.gcc -o hellomake hellomake.c hellofunc.c -I.

LISTA.C

#include #include

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 heaphead->data = 1;head->next = second; // setup first node// note: pointer assignment rulesecond->data = 2;second->next = third; // setup second nodethird->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/bashfor i in {100..110}doecho $i >> t.txtgcc z.c./a.out < t.txt > $i".txt"rm t.txtecho "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"echo "PARA EL CASO M >> N"gcc Mn.ctime ./a.out < $i".txt"./a.out < $i".txt"echo "-----------"echo "PARA EL CASO N >> M"gcc Nm.ctime ./a.out < $i".txt"./a.out < $i".txt"echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"done

#include

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));}

#includemain(){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 single-chip, 16-bit microproces-sors. The 8088, a variant of the 8086 with an 8-bit external bus, formedthe heart of the original IBM personal computers. IBM contracted withthen-tiny Microsoft to develop the MS-DOS operating system. The orig-inal models came with 32,768 bytes of memory and two floppy drives (nohard drive). Architecturally, the machines were limited to a 655,360-byteaddress spaceaddresses were only 20 bits long (1,048,576 bytes address-able), and the operating system reserved 393,216 bytes for its own use.In 1980, Intel introduced the 8087 floating-point coprocessor (45 K tran-sistors) to operate alongside an 8086 or 8088 processor, executing thefloating-point instructions. The 8087 established the floating-point modelfor the x86 line, often referred to as x87.80286: (1982, 134 K transistors). Added more (and now obsolete) addressingmodes. Formed the basis of the IBM PC-AT personal computer, theoriginal platform for MS Windows.i386: (1985, 275 K transistors). Expanded the architecture to 32 bits. Added theflat addressing model used by Linux and recent versions of the Windowsfamily of operating system. This was the first machine in the series thatcould support a Unix operating system.i486: (1989, 1.2 M transistors). Improved performance and integrated thefloating-point unit onto the processor chip but did not significantly changethe instruction set.Pentium: (1993, 3.1 M transistors). Improved performance, but only addedminor extensions to the instruction set.PentiumPro: (1995, 5.5 M transistors). Introduced a radically new processordesign, internally known as the P6 microarchitecture. Added a class ofconditional 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 instructionsfor manipulating vectors of integer or floating-point data. Each datum canbe 1, 2, or 4 bytes, packed into vectors of 128 bits. Later versions of thischip went up to 24 M transistors, due to the incorporation of the level-2cache on chip.Pentium 4: (2000, 42 M transistors). Extended SSE to SSE2, adding new datatypes (including double-precision floating point), along with 144 newinstructions for these formats. With these extensions, compilers can useSSE instructions, rather than x87 instructions, to compile floating-pointcode. Introduced the NetBurst microarchitecture, which could operate atvery high clock speeds, but at the cost of high power consumption.157158Chapter 3 Machine-Level Representation of ProgramsPentium 4E: (2004, 125 M transistors). Added hyperthreading, a method to runtwo programs simultaneously on a single processor, as well as EM64T,Intels implementation of a 64-bit extension to IA32 developed by Ad-vanced Micro Devices (AMD), which we refer to as x86-64.Core 2: (2006, 291 M transistors). Returned back to a microarchitecture similarto P6. First multi-core Intel microprocessor, where multiple processors areimplemented on a single chip. Did not support hyperthreading.Core i7: (2008, 781 M transistors). Incorporated both hyperthreading andmulti-core, with the initial version supporting two executing programson each core and up to four cores on each chip.

void inplace_swap(int *x, int *y) {*y = *x ^ *y; /**x = *x ^ *y; /**y = *x ^ *y; /*}