- Crystal

advertisement
CSE 1320
Basics
Dr. Sajib Datta
CSE@UTA
Course Details
 The website is up.
 Course lectures will be uploaded there
 Check regularly for assignments and update
Programming Platform
 You have to execute your code on Omega server
 Connect Omega Server using SSH
 If you are using Macbook or Linux (such as Ubuntu), then open
Terminal, and commend ssh your_UTA_NET_ID@omega.uta.edu.
Then it will ask for password (UTA NetID password).
 If you are using Windows, please download SSH (Secure Sheel
Client)
 http://www.uta.edu/oit/cs/unix/ssh/Secure-Shell-Client.php
 Download Code:Blocks:
 http://www.codeblocks.org/
Open source, cross platform, free IDE
Using Omega Server
 A Linux server
 Get an account!
 http://www.uta.edu/oit/cs/web/omega-web.php
 Provides C, C++, Lisp, Prolog, Cobol, and
Fortran language compilers
 Connect using SSH
http://www.uta.edu/oit/cs/files/sftp/ssh/index.php
Using Omega
 Windows users: download SSH client from OIT
 http://www.uta.edu/oit/cs/unix/ssh/Secure-Shell-
Client.php

Connecting to Omega
Provide Login Information
 omega.uta.edu
Provide Password
File Transfer
Omega Terminal
From MAC/Linux
 Applications -> Utilities -> Terminal
 Login:
 $ ssh your_netID@omega.uta.edu
 Logout:
 $ logout<Return>
Some UNIX commands
 ls : displays the files in a specified directory
 [vxg4212@omega ~]$ ls
 rm : delete a file
 [vxg4212@omega ~]$ rm file_name
Some UNIX commands
 mv : rename files
 [vxg4212@omega ~]$ mv old_filename new_filename
 [vxg4212@omega ~]$ mv old_filename
directory/new_name
More…
 pwd -> print the current (working) directory
 cd -> change directory
 cd .. ->go up one level
 mkdir -> make a new directory
 rmdir -> remove a directory
 Note: Commands can have options. Use man
command_statement to get full details
Working with VI
 vi editor
 [vxg4212@omega ~]$ vi file_name.extension
 Two modes: Command mode and Insert mode
Working with VI
 Insert mode: entered text is inserted into the file
 Hit i to get into insert mode
 Hit Esc to get out of insert mode
 :x <Return> to exit vi
 Whole lot of options available in insert mode!
 For more on vi please visit:
http://www.cs.colostate.edu/helpdocs/vi.html
C compiler
 gcc : compile a C program
 vxg4212@omega ~]$ gcc -o my_out program_file.c
 Run the compiled program
 vxg4212@omega ~]$ my_out<Return>
Introduction to C
Variables, Statements, Operators,
Expressions
Basics
 Storing information - Variables
 Statements - Declaration, Assignment, Function
 Making decisions – Conditionals
Variable declaration
int num;
Assigning a value to the variable
num = 51;
if (num > 25) conditional
Print function
printf(“a large class. \n”);
conditional
else
Print function
printf(“a small class. \n”);
 Repeating certain tasks – Loops
 Print “CSE 1311003” 1000 times
Variables
 A variable is a symbolic name used to represent some
information – data
 A variable is associated with a type, data storage location, its
content which can be changed in running the program
Variables
 C requires the programmer to specify the
type of variable.
Some examples are:
 int – used for integers,
 A number with no fractional part
 Never with a decimal point
 3, 14, -13, 7
 double – used for floating point numbers
 A real number including numbers between the integers
 0.5, 1.234
 char – used for characters, e.g., ‘A’, ‘7’, ‘?’
Naming a variable
 Lowercase letters, uppercase letters, digits,
and underscore
 First character must be a letter or an underscore
 Not key words
Variables
 Declare a variable:
 int weight;
 int height;
 int weight, height;
 Different variable types use different amounts
of memory
/* reports the amount of memory occupied by an int on this
hardware */
printf("an int takes %d bytes of memory\n",sizeof(int));
Variables
 Declare a variable
 int height; [note that no value is still assigned]
 Assign a variable a value
 height = 5;
 Define - Declare & Initialize
 int height = 5;
 char name = ‘a’;
 double marks= 90.3;
Variables
 Print a variable
 printf(“%d”, num); - format specifier, variable
 Number matching - the number of specifiers is equal to the number
of variables
 Type matching
 %d for int
 %lf for double
 %c for char
 printf can be used just to print text
 printf(“this is just a text”);
Statements
 A C program consists of statements
 Some types of statements:
 declaration statements
 Assignment statements
 Function calls
 Control statements
 Each statement is terminated by a semicolon.
 Control statement can change the program flow.
Declaration
 Declaration statements are when we declare
variables for use.
void main()
{
int a;
……
}
Assignment
 Have a left side and a right side.
 The right side:
 a single value, a complicated expression, or a function call
 ultimately reduce to a single value, which is then assigned to the
variable named on the left side.
 Example:
 int num; [declaration]
 num = 1;
 num = num + 10;
 num = num + 2; [what is the final value of num?]
Operators
 The basic operators that you have in math are also available in C:





+,
-,
*,
/,
=
 WARNING: Difference between operators in C and their math use is
integer division.
 The fraction resulting is truncated in integer division
 integer = integer / integer
 Example:
 int a = 7, b = 5;
 int answer;
 answer = a / b;
Think!
float a = 7.1, b = 5.2;
int answer;
answer = a / b;
Modulus
 A new operator used in C is modulus operator: %
 % only used for integers, not floating-point
 Gives the integer remainder from integer division.
 Example:
 int a = 7, b = 3;
 int answer;
 answer = a % b;
More…
The following assignment operators are
available in C:
+= addition
-= subtraction
*= multiplication
/= division
num = num + 2 same as num+=2;
num = num - 1 same as num-=1;
 What is the output of ‘x’?
 int x;
 x = 10;
 x += x;
 printf(“%d”, x);
Recap…
 We discussed:
 Omega
 Variable
 Declaration, assigning a value, initialization
 Statements
 Conditional statements can change the flow of the code
 Operators
 +,-,*,%
 Data type
 What happens when you assign a float/double to a variable of type int
Operator Precedence
 A precedence for each operator
 Multiplication and division have a higher precedence than addition and
subtraction
 For operators with the same precedence, if they share an operand, they
are executed according to the order they occur in the statement. (In
most cases, from the left to the right, except assignment operation)
 Examples:
 int num, x;
 num = 2;
 x = 1 + 3*num/4;
What is the value of x?
Operator Precedence
 Want an addition operation to take place before division?
 int num, x;
 num = 2;
 x = (1 + 3)*num/4;
 What is x now?
 int a = 1, b = 4;
 b += a+2; is equivalent to b = b + (a + 2)
Relational Operators
 <






: is less than
<= : is less than or equal to
== : is equal to
>= : is greater than or equal to
> : is greater than
!= : is not equal to
Example:
 Relational expression: a > 10
 If the relation is true, the value of the expression
is 1, otherwise 0.
Expressions
 A combination of operators and operands, where operands




can be constants, variables, or combinations of the two
4
4+21
a = (b+c)/2
q>4
Statement vs. Expression
 A statement is a complete instruction to the computer
 In C, it is indicated by semicolon
 An statement is consists of expressions
Increment and Decrement Operator






++ and –The operand must be a variable
Two varieties – prefix and postfix
++ a, --b
a ++, b -This operation increases(decreases) the value of its operand by just one!
 In the prefix form, the increment or decrement takes place before the
value is used in expression evaluation
 so the value of the expression is different from the value of the operand.
 In the postfix form, the increment or decrement takes place after the
value is used in expression evaluation
 so the value of the expression is the same as the value of the operand
Increment and Decrement Operator
 Case1:
 a = 3;
 b = 2* ++a;// the value of the expression ++a is the new
value of a, therefore b = ?
 Case2:
 a = 3;
 b = 2* a++;// the value of the expression a++ is the old value
of a, therefore, b = ?
Conditionals
if condition
if statement gives you choice of either executing a statement or skipping it
 The basic format of the if statement is

 if (condition_is_true)
do_something;
// condition_is_true should be an expression
//need the parenthesis for the expression
// If expression evaluates to true (nonzero), do something. Otherwise, it is
skipped.
Normally, expression is relational expression. But in general, any expression will work since it has a
value, which can be mapped to true or false.

Examples

int x;

int y = 6;

scanf (“%d”, &x);

if (x >= 2)

y = 10;

printf(“y is %d.\n”, y);
if…
 Examples
int x;
int y = 6;
scanf (“%d”, &x);
if (x >= 2)
y = 10;
printf(“y is %d.\n”, y);
Download