Vous êtes sur la page 1sur 32

6.

179: Intro to C/C++


Lecture 2

Get Started
/* includes go here*/
int main() {
/* code goes here */
return 0;
}

Pointers
A pointer stores a memory address
int *pointer_to_int;
char *pointer_to_char;
int x;
int *p;
p = &x;
More in next lecture, on memory

Arrays
/* integer array */
int numbers[100];
/* character array, a.k.a. string */
char string[50];
/* 2D array */
int matrix[10][10];

Strings strlen
#include <string.h>
size_t strlen(const char *str)
int x = strlen("Hello World");
/* value of x is 11 */

Strings strcpy
char *strcpy(char *dest,
const char *src)
char src[50];
char dest[50];
strcpy(src, "Hello World");
strcpy(dest, src);
/* src & dest are "Hello World" */

Strings strcat
char *strcat(char *dest,
const char *src)
char dest[50];
strcpy(dest, "Hello ");
/* dest is "Hello " */
strcat(dest, "World");
/* dest is "Hello World" */

Strings strcmp
int strcmp(const char *str1,
const char *str2)
char str1[50];
char str2[50];
// put strings in str1 & str2
if (strcmp(str1, str2) == 0) {
/* str1 is equal to str2 */
}

Console I/O
All of our test cases use this!
Get inputs from stdin (standard input)
Push outputs to stdout (standard output)
#include <stdio.h>

stdin getchar
int getchar(void)
/* character is cast to int */
int c;
c = getchar();
/* you can cast it back */
char c;
c = getchar();

stdin gets
char *gets(char *str)
/* create storage for string */
char str[50];
/* return value can be ignored */
gets(str);

stdin scanf
int scanf(const char *format, ...)
/* expecting a string */
char str[50];
scanf("%s", str);
/* expecting 2 integers */
int x, y;
scanf("%d %d", &x, &y);

stdout putchar
int putchar(int char)
/* ASCII code */
putchar(97);
/* character literal */
putchar('a');

stdout puts
int puts(const char *str)
/* C-style string */
char str[50];
strcpy(str, "Hello World");
puts(str);
/* string literal */
puts("Hello World");

stdout printf
int printf(const char *format, ...)
/* string literal */
printf("Hello World");
/* with format specifier */
int x = 1;
printf("x equals %d", x);

Newlines
puts("Hello World");
printf("Hello World\n");
printf("H");
printf("e");
printf("l");
printf("l");
printf("o");

Easy Problem
Add N (1 <= N <= 100,000) positive numbers
below 100.
INPUT FORMAT:
Line 1: A single integer, N.
Line 2N+1: An integer to add.
OUTPUT FORMAT:
Line 1: The sum of the N integers.

Easy Problem get N


int N;
scanf("%d", &N);

Easy Problem add things up


int i, sum = 0;
for (i = 0; i < N; i++) {
int a;
scanf("%d", &a);
sum += a;
}

Easy Problem output solution


printf("%d", sum);

Easy Problem
int N, i, sum = 0;
scanf("%d", &N);
for (i = 0; i < N; i++) {
int a;
scanf("%d", &a);
sum += a;
}
printf("%d", sum);

Fib Numbers
Find the N-th (1 <= N <= 500,000) Fibonacci
number.
INPUT FORMAT:
Line 1: A single integer, N.
OUTPUT FORMAT:
Line 1: The N-th Fibonacci number, modulo
1,000,000,007.

Fib Numbers start


int N, result;
scanf("%d", &N);
/* calculate result here */
printf("%d", result);

Fib Numbers small N


if (N < 2) {
result = 1;
} else {
/* calculate fib sequence */
}

Fib Numbers sequence


int fib[N+1];
fib[0] = fib[1] = 1;
int i;
for (i = 2; i <= N; i++) {
fib[i] = (fib[i-1] + fib[i-2]);
fib[i] %= 1000000007;
}
result = fib[N];

Fib Numbers
int N, i, result;
scanf("%d", &N);
if (N < 2) {
result = 1;
} else {
int fib[N+1];
fib[0] = fib[1] = 1;
for (i = 2; i <= N; i++) {
fib[i] = (fib[i-1] + fib[i-2]);
fib[i] %= 1000000007;
}
result = fib[N];
}
printf("%d", result);

Stacking Cups
Numbers 1 to N are uniquely assigned to cups. PUSH puts
a cup on top of the stack; POP takes the topmost cup off. A
cup may be pushed only once. Given N (1 <= N <=
1,000,000), the number of cups, and M (0 <= M <= 2N),
the number of operations, determine the result given the
input sequence.
INPUT FORMAT:
Line 1: Two integers, N and M.
Line 2...M+1: Either PUSH [integer from 1 to N] or POP.
All operations are guaranteed to be valid.
OUTPUT FORMAT:
Line k: The cup popped by the k-th POP input line.

Stacking Cups setup


/* get N and M */
int N, M;
scanf("%d %d", &N, &M);
/* make a stack */
int stack[N];
int index = 0;

Stacking Cups iterate


/* string for PUSH or POP */
char command[5];
int cup, i;
for (i = 0; i < M; i++) {
/* process move i */
}

Stacking Cups process move


scanf("%s", command);
if (strcmp(command, "PUSH") == 0) {
scanf("%d", &cup);
stack[index++] = cup;
} else {
printf("%d\n", stack[--index]);
}

Stacking Cups
int N, M, cup, i, index = 0;
scanf("%d %d", &N, &M);
int stack[N];
char command[5];
for (i = 0; i < M; i++) {
scanf("%s", command);
if (strcmp(command, "PUSH") == 0) {
scanf("%d", &cup);
stack[index++] = cup;
} else {
printf("%d\n", stack[--index]);
}
}

Math
#include <math.h>
/* exponents */
double pow(double x, double y)
double x = pow(5.0, 2.0); // 5 ^ 2
/* square root */
double sqrt(double x)
double x = sqrt(25.0); // x = 5

Vous aimerez peut-être aussi