0% found this document useful (0 votes)
71 views28 pages

01 Syllabus PDF

The function prototype syntax allows for mutual recursion between functions. Without prototypes, the compiler would not know that go() and stanford() call each other recursively and would report errors. Prototypes declare the functions ahead of time so the compiler understands their interfaces and can accept recursive calls between them.

Uploaded by

gaurav singh
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)
71 views28 pages

01 Syllabus PDF

The function prototype syntax allows for mutual recursion between functions. Without prototypes, the compiler would not know that go() and stanford() call each other recursively and would report errors. Prototypes declare the functions ahead of time so the compiler understands their interfaces and can accept recursive calls between them.

Uploaded by

gaurav singh
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/ 28

CS 106X –

Programming
Abstractions in
C++
Dr. Cynthia Bailey Lee
2

Today’s Topics
1. Course structure and procedures:
 Your Grade
 My role
 Your role
2. What is this class? What do we mean by
“abstractions”?
Next lecture:
 Introduce the C++ language from the Java
programmer’s perspective
 (But it’s ok if you are not a Java programmer)
3

Who Am I? Cynthia Lee


 Research:
 PhD: Supercomputers
 Lately: improve computer science
education
 Teaching:
 5 years at UCSD
 Courses: architecture (hardware),
theory of computation, discrete
mathematics, beginning &
advanced Java, data structures,
MATLAB
 Software engineer:
 iPhone educational games
 Machine learning for search
engines
 When I’m not working:
 Climbing, hiking, volleyball
4

The Basics: Your Grade


 6% Class participation
 4% Section participation
 20% Midterm
 30% Final
 40% Programming assignments
5

What do I do in class?
 Think of me as your tutor
 Many of you, the programming
aficionados of the incoming class, are
partially or entirely self-taught
 This course is designed to continue the most
beneficial aspects of that learning style, but
in a structured/assisted way, ensuring we fill
in any gaps before you continue on
6

What do I do in class?
 That means that what I do is:
 Be your guide in inducing you to explore
concepts
 Create situations and pose puzzles that set
the scene for your exploration
 Answer your questions
 Not spend lecture reading the textbook to
you with slightly different words
7

What do you do in class?


(Before class, you prepare yourself by reading the
textbook/handouts)
1. I ask a question
8

What do you do in class?


(Before class, you prepare yourself by reading the
textbook/handouts)
1. I ask a question
2. You first answer it by yourself
9

What do you do in class?


(Before class, you prepare yourself by reading the
textbook/handouts)
1. I ask a question
2. You first answer it by yourself
3. Then discuss in assigned groups of 3-4 students
 Like a jury, you must come to a unanimous
decision
 Answer the question a second time
10

What do you do in class?


(Before class, you prepare yourself by reading the
textbook/handouts)
1. I ask a question
2. You first answer it by yourself
3. Then discuss in assigned groups of 3-4 students
 Like a jury, you must come to a unanimous
decision
 Answer the question a second time
4. I will ask groups to share their insights, and I will
provide additional clarification as needed
11

FAQ: “But Professor Lee, wouldn’t it be


more efficient if you just taught us the right
answer to begin with?”
 Have you ever heard of an aerobics class where
the instructor did all the exercises at the front of
class, while the class just watched attentively?
 Me neither.

 To
learn, you must do the work with your own
muscle (your brain).
12

What do you do in this course?


 Prepare for maximum in-class learning
 Reading, reading quizzes
 Inclass: engage with your neighbors and the
class, engage with the ideas
 Turn them upside down and sideways, think about
what common errors or misconceptions might be
 Seek help and seek to help others
 In class, section, Piazza forum, section leader office
hours, instructor office hours, LaIR
 I expect each class member to contribute to an
environment of mutual aid and cooperation
13

Who should you be in this course?


 Be a professional
 Take the time to truly learn and transform yourself into a
professional, not “cram” or work for points
 Be warm and generous
 Be invested in the progress and well-being of your
classmates
 Be earnest
 Let your participation to be characterized by risk-taking and
a headlong dive into the activity. Free yourself of the need
to withhold part of yourself or remain aloof.
 Be honest
 Decide today to follow a path of uncompromised integrity
in your work in this course and throughout your Stanford
career. Envision yourself in the future with the confidence
that can only be purchased with a long record of
scrupulous decision-making.
About the textbook
 Required textbook:
 Programming Abstractions in C++ by Eric
Roberts
 Prof. Roberts is a faculty member here at
Stanford
 Note: do not buy C version
What is this class about?
What do we mean by “abstractions”?
http://www.publicdomainpictures.net/pictures/10000/velka/1-1265899974oKJ9.jpg
http://www.publicdomainpictures.net/pictures/10000/velka/1-1265899974oKJ9.jpg
Sentence

Subject Verb Phrase Object

Noun Adverb Verb Possessive Noun

CS106X totally rocks my socks


CS106X totally rocks my socks
Building a vocabulary of abstractions
makes it possible to represent and solve a huge
variety of problems using known tools.
21

Getting started to-do list:


 Ensure you are on our mailing list
 Ensure you are on Piazza
 Register your iClicker at http://iclicker.com
 Use SUNet ID
Introduction to C++
From the Java programmer’s perspective
23

A first C++ program (ERROR)


firstprogram.cpp
#include <iostream>
#include "console.h"
using namespace std;

int main(){
cout << "|-5| = " << absoluteValue(-5) << endl;

return 0;
}

int absoluteValue(int n) {
if (n<0){
return -n;
}
return n;
}
24

A first C++ program (FIXED 1)


firstprogram.cpp
#include <iostream>
#include "console.h"
using namespace std;

int absoluteValue(int n) {
if (n<0){
return -n;
}
return n;
}

int main(){
cout << "|-5| = " << absoluteValue(-5) << endl;

return 0;
}
25

A first C++ program (FIXED 2)


firstprogram.cpp
#include <iostream>
#include "console.h"
using namespace std;

int absoluteValue(int n);

int main(){
cout << "|-5| = " << absoluteValue(-5) << endl;

return 0;
}

int absoluteValue(int n) {
if (n<0){
return -n;
}
return n;
}
26

Best practices for a good


group discussion
 Formally take turns being the first one to talk
 Once you all agree on the answer, don’t stop!
 Even if your group-mate has said something very
clearly and correctly, I encourage you to repeat it
yourself
 “So, what I think you said was, …”
 Perhaps appears pointless, but your brain will
remember it better
 Pro tip: reverse engineer my question design—how
was I hoping to “trick” somebody by including that
wrong answer? Why might someone be temped by it?
Question: Why does C++ have
the function prototype syntax?
 In other words, why not just have a rule that you
must set up the ordering so you define your
functions before using them, as in the “FIXED 1”
example?
A. C++ could have done that, but such a rule would
be cumbersome.
B. C++ could have done that, but good
programming practice dictates “top-down”
approach that logically puts main() first and
helper functions it calls to follow.
C. C++ couldn’t have done that—sometimes there is
no way to order the functions so that all functions
are defined before being used.
D. All/none/more than one of the above
Mutual recursion
#include<iostream>
#include "simpio.h"
#include "console.h"
using namespace std;

void go(int n);


void stanford(int n);

int main(){
int n = getInteger("Enter a number of times to repeat: ");
go(n);

return 0;
}

void go(int n) {
if (n == 0) {
return;
}
cout << "Go!" << endl;
stanford(n-1);
}

void stanford(int n) {
cout << "Stanford!" << endl;
go(n);
}

You might also like