0% found this document useful (0 votes)
56 views3 pages

Quiz 2 - CS50

The document contains 3 questions about C programming concepts related to strings and command line arguments. It asks about programs that accept command line arguments like the make program. It describes that argc stores the number of arguments as an integer, and argv stores the arguments as an array of character strings. Regarding for loops to print characters of a string, it notes the second version is more efficient as it only calls strlen() once to store the length rather than on every iteration like the first version.

Uploaded by

OLT IMERI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

Quiz 2 - CS50

The document contains 3 questions about C programming concepts related to strings and command line arguments. It asks about programs that accept command line arguments like the make program. It describes that argc stores the number of arguments as an integer, and argv stores the arguments as an array of character strings. Regarding for loops to print characters of a string, it notes the second version is more efficient as it only calls strlen() once to store the length rather than on every iteration like the first version.

Uploaded by

OLT IMERI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1 of 3

a) Suppose that you've just received the text message below.

Assuming the text message is represented using ASCII, what sequence of bits
(or, if you prefer, decimal digits) did you actually receive?

b) Suppose that you've just received the below text message instead.

Assuming the text message is represented using ASCII, what sequence of bits
(or, if you prefer, decimal digits) did you actually receive?

c) Why are those sequences not the same?

Answers

a) The sequence of bits would be: 1001000 1001001 0100001 aka 72,73, 33
b) The sequence of bits would be: 0110111 0110010 0110111 0110001 0110001
0110001 aka. 55 50 , 55, 51 ,51 51
c) Because in ASCII numbers are not represented by as their bit counterparts and
each digit is represented as an individual character therefore numbers from 0-9
are assigned values 48-57.
Question 2 of 3

Recall that, in lecture, we saw how to write programs in C that support command-line
arguments. To do so, we modified the program's main function to take two arguments:
argc and argv.

a) What's a program you've used already in CS50 that accepts command-line


arguments? Name the program and describe what the command-line arguments
are used for.
b) What is stored in argc? What is its type?
c) What is stored in argv? What is its type?

Answers

a) Maybe one could be the make program as it uses the command line argument to
know the name of the c program file to make the compiled code
b) argc is the argument counter which counts/denotes the number of arguments
given, it itself is an integer variable
c) argv is used as a parameter to represent commandline arguments as an array
list of arguments, as char or arrays of chars, so strimngs, or arrays of strings
from argv[0] to argv[argc-1] the last argument
Question 3 of 3

Recall that, in lecture, we saw the following two for loops, both of which print the
characters of a string, s, one character per line.

Version 1 Version 2

for (int i = 0; i < strlen(s); i++) for (int i = 0, n = strlen(s); i < n; i++)
{ {
printf("%c\n", s[i]); printf("%c\n", s[i]);
} }

a) In your own words, what is a string?


b) In both of the above functions, the function strlen is used to get the length of
the string s. Given what you know about how strings are represented in C, how
does strlen likely compute the length of the string?
c) Which of these two versions of the code is more efficient? In what way is it more
efficient?

Answers

a) A string is just an array with characters in many programing language string is


used as a seperate veriable but it’s still built as an array, so it’s a class that has
it’s own methods/functions which you can recall for example the length of the
string.
b) It most likely counts the array using a for loop, until it meets the exception whre it
doesn’t return a character after the last position, so returning null.
c) The second version since the function strlen() is run once and not every time like
in the first version where it has to be to check whether i is smaller or not.

You might also like