Week 2 notes - Notes 2 Counting Lines -> Count Carriage...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Notes 2 Counting Lines --> Count Carriage Return Counting Words o Sequence of characters separated by one or more spaces ' ' or new lines '\n' o You can use an array of characters to store the word when you are reading the characters before displaying it. #define MAXWORD 100 char word [MAXWORD]; i=0; while (c = getchar()) != EOF) { word [i] = c i++; } else { //end of word if (i>0) { //there is a word hello[i] = 0; //add //null character i=0; //end printf("%s\n", word); //start word //new word RPN calculator o implement a stack function o push (x) o x = pop() Global variables o #define MAXSTACK 500 o double stack [MAXSTACK]; o int top=0; o void push (double x) { o if (top == MAXSTACK) { o printf("stack full\n");
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
o exit(1); o } o stack [top] = x; o top++; o } o double pop() { o if (top == 0) { o printf("stack empty\n"); o exit(1); o } o top--; o double val = stack[top]; o return val; o } o main (int argc, char **argv) { o for (i=1; i<argc; i++) { o //check if arg is a number o o //this is a number o double val = atof(argv[i]); //atof converts a string to a float o push (val); o or you can also use sscanf to convert strings to numbers. o int i; o int n; //args read o double x; o n = sscanf(str, "%d", &i); o //read an integer from str. n is 0 if error n is 1 if okay o n = number of args read o n = sscanf(str, "%lf", &x); o //reads a double and puts it in x. n=1 is okay, n=0 equals error o else if (strcmp(argv[i], "+") == 0) { o //plus sign o double a = pop(); o double b = pop(); o push (a+b); o //then other operations o printf("%lf", result);
Background image of page 2
one error you can get is stack underflow when implementing a stack In a computer memory is stored as a sequence of bytes. A byte is a group of 8 bits. In a processor that has registers with 32 bits long. Registers in the cpu are 32 bits long, in a 32 bit computer or 64 bit long in a 64 bit computer. We will assume a 32 bit computer 32 bits = 4 bytes 2 32 minus 1 --> largest address 0 --> smallest address o 2 10 = 1024 = 1KB o 2 20 = 1024 x 1024 = 1MB o 2 30 = 1024 x 1024 x1024 = 1GB o 2 32 address, a cpu 32 bits can address 2 2 x 2 30 = 4GB 4GB is the theoretical limit of memory that you can have in a program running in a 32 bit CPU. o this includes your program, plus variables plus a stack and plus an operating system everything is stored in memory C will give you the tools to manipulate memory a pointer --> allows you to store something at a specific address Tuesday, June 21, 2011 This is a reminder that project 1 is due Wednesday, June 22, 2011 by 11:59PM. New project is due Wednesday, June 29, 2011 by 11:59PM.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/18/2012 for the course CS 240 taught by Professor Rego during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 15

Week 2 notes - Notes 2 Counting Lines -&gt; Count Carriage...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online