You are on page 1of 5

Week 2

Shreyvardhan Sharma
Tuesdays, 4 30pm, Boylston 104
sharma_s@college.harvard.edu

Arrays
Arrays are data structures that can store fixed-size sequential collection of
elements of the same type. Indexing starts with 0, as is the norm in CS. Not
only are arrays more efficient (than declaring a bunch of variables separately),
but their utility lies in being able to store and access data seamlessly at any
stage of the program.
Declaring arrays: type name[size] , for example,
int scores[5]

Values can be assigned to an array one-by-one, such as scores[0] = 5, or


(more efficiently) using a loop, such as:
for

for (int i = 0; i < 5; i++) {


scores[i] = i;
}
// scores[] = [0, 1, 2, 3, 4]

Declaring (and initialising arrays) so as to input data from the user is similar:
int scores[3];
for (int i = 0; i < 3; i++)
{
scores[i] = get_int("Score: ");
}

Strings
A string can be thought of as an array of characters. Although C does not have
an inbuilt data type for strings, the header file allows us to use
cs50.h string

as a data type for our programs, with handy functions such as .


get_string

Week 2 1
Strings are terminated with a special character: , to demarcate the end of
\0

the string. Knowing this is particularly useful when we want to iterate through
a string: the last character of the string will always be . The is not
\0 \0

counted in the length of the string while calling on it.


strlen

String declaration: . For this string, refers to the


string s = "cs50" s[0]

character at index zero, which is ; refers to the character at index one,


c s[1]

which is , and so on.


s

Command Line Arguments


Command line arguments are arguments passed along with the name of the
program while executing the program in CS50 IDE.
int main(int argc, string argv[])
{
...
}

We typically define main() with two arguments: the first argument, is an argc

integer storing the number of command line arguments. The second argument,
argv is an array of strings storing a list of the command line arguments
themselves. For example, executing a program in CS50 IDE with
caesar.c

./caesar 2 will give a value of (because there were 2 command line


argc 2

arguments passed, including the part); will then be an string-


./caesar argv

type array of size two — referring to the string


argv[0] and
./caesar argv[1]

referring to the string . 2

IMPORTANT argvis an array of type . Therefore, if we pass in an any


string

numerical data as a command line argument; we often need to convert that


numerical data from a string to an int to be able to operate on it. This is done
with the help of the function , which converts a string to an int. For
atoi

example, atoi("2")would evaluate to simply the integer . 2

Example 1: Addition of 2 user-provided


numbers
Sample Usage:

Week 2 2
$ ./addition 2 8
2 + 8 = 10

$ ./addition 2
Usage: ./addition x y

Solution:
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, string argv[])


{
if (argc != 3) // If 3 command line arguments are not provided, exit the program
{
printf("Usage: ./addition x y\n");
return 1; // exit the program
}

int x = atoi(argv[1]); // convert string to integer


int y = atoi(argv[2]); // convert string to integer

printf("%i + %i = %i\n", x, y, x + y);

Couple of things to note here:


1. We're looking for precisely three command line arguments because we
wants the name of the program (i.e, ) followed by 2 numbers. If
./addition

there are less (or more) than 3 arguments, we want to exit the program. A
simple way to do this is to return 1from within the function.
main int

main returns by default, indicating that the program ran successfully, and
0

we can return 1 to exit the program midway in case any condition is not
met.
2. To add the two numbers inputted from the user, the numbers need to be of
type . The numbers obtained from the command line are of type
int

string . To be able to add them, we use to convert a


atoi into an
string

int.
3. is a placeholder for variables of type .
%i int

Example 2: Palindrome
Week 2 3
Write a program that takes a string as input, and determines
palindrome.c

whether it is a palindrome (the same backwards and forwards).


Sample usage:
$ ./palindrome
Text: racecar
PALINDROME

$ ./palindrome
Text: jellyfish
NOT PALINDROME

Solution:
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
string s = get_string("Text: ");

bool palindrome = true;

// Check characters from start and end


for (int i = 0, len = strlen(s); i < len / 2; i++)
{
if (s[i] != s[len - 1 - i])
{
palindrome = false;
}
}

// Print output
if (palindrome)
{
printf("PALINDROME\n");
}
else
{
printf("NOT PALINDROME\n");
}

Example 3: Initials
Week 2 4
Write a program that takes a user’s full name as input, and outputs
initials.c

their initials. The program should accept a user’s name using . Initials
get_string

should all be printed as uppercase letters, even if the name contains lowercase
letters. You may assume that the user’s names will be separated by one space.
Sample usage:
$ ./initials
Name: David J. Malan
DJM

Solution:
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
string name = get_string("Name: ");

// Keep track of whether to output next character


bool output = true;

// Loop over all characters in name


for (int i = 0, len = strlen(name); i < len; i++)
{
// Check if we should output this character
if (output == true)
{
printf("%c", toupper(name[i]));
output = false;
}

// If we encounter a space, output the next character


if (name[i] == ' ')
{
output = true;
}
}

printf("\n");
}

Week 2 5

You might also like