0% found this document useful (0 votes)
26 views

Beginning C Programming For Engineers (CSCI-1190)

Here is a program to calculate the average of three floating point numbers (2.3, 3.4, 4.5) using scanf and assignment: 1. #include <stdio.h> 2. 3. int main() { 4. float num1, num2, num3, average; 5. num1 = 2.3; 6. num2 = 3.4; 7. num3 = 4.5; 8. average = (num1 + num2 + num3) / 3; 9. printf("The average is: %.2f\n", average); 10. return 0; 11.

Uploaded by

Abdul Wahab
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Beginning C Programming For Engineers (CSCI-1190)

Here is a program to calculate the average of three floating point numbers (2.3, 3.4, 4.5) using scanf and assignment: 1. #include <stdio.h> 2. 3. int main() { 4. float num1, num2, num3, average; 5. num1 = 2.3; 6. num2 = 3.4; 7. num3 = 4.5; 8. average = (num1 + num2 + num3) / 3; 9. printf("The average is: %.2f\n", average); 10. return 0; 11.

Uploaded by

Abdul Wahab
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Beginning C Programming for

Engineers (CSCI-1190)

Instructor: Gang Chen ([email protected])


Office Hour: Thursday 2-4pm (or by appointment)
Office: Lally 9A
Webpage: www.cs.rpi.edu/~cheng3/teaching/1190
Basic Computer Model
Input CPU Output

Memory

Instructions
+
Data

Von Neumann Architecture


Programming Languages
+1300042774
Machine Languages +1400593419
+1200274027

LOAD A
Assembly Languages ADD B
STORE C

High-Level Languages C=A+B


Creating Programs

C libaray

Edit hello.c compile hello.o Link hello

Source File Object File


Executable
(High-Level (Machine Languages)
Languages)

nedit hello.c gcc –o hello hello.c


Unix Commands
Command Operation
ls List files
cp fromFile
Copy fromFile to toFile
toFile
mv fromFile
Move fromFile to toFile
toFile
rm theFile Remove theFile
mkdir theDir Create theDir
cd theDir Change directory to theDir
cat theFile Display theFile
Display theFile one page at
more theFile a time
lpr printFile Print theFile
Hello, World
1. /* Hello, world program */
2. #include <stdio.h>
3.
4. int main()
5. {
6. printf("hello, ");
7. printf("world\n");
8. return 0;
9. }
• Editing: xemacs hw.c& or nedit hw.c& or vi hw.c
• Compiling: gcc –Wall –o hello hw.c
• Executing: hello
Basic Programming Concepts
• Comment To explain what the program is for and why it is
the way it is.
• Preprocessing Done before the program is compiled.
• To include header files containing information about C libraries
• Statement A line of code that tells computer to perform
some action. Always ended with a semicolon(;).
• Invoking functions
• Function A module often consisting of a set of
statements.
• Two functions in the hello world program: main, printf
In Class Exercise 1-1
• Escape Sequences are used to control
printf to do something other than printing
characters.
• Modify the hello world program to try out
various escape sequences like:
• \n: Newline. Position cursor at the beginning
of the next line.
• \t: Tab. Move cursor to the next tab stop.
• \a: Alert. Sound the system bell.
Arithmetic Operators
• To form expressions + Addition
• Many statements are merely - Subtraction
expressions. Multiplicatio
*
• Normal order of operations is n
followed. Parentheses can / Division
be used. Modulus
% (remainder)
• If you are not sure, look up
++ Increment
in Appendix C. Operator -- Decrement
Precedence Charts, p1193
Arithmetic Operators: An Example
1. /* Arithmetic operators */
2. #include <stdio.h>
3.
4. int main()
5. {
6. printf("7+3=%d\n",7+3);
7. printf("7-3=%d\n",7-3);
8. printf("7*3=%d\n",7*3);
9. printf("7/3=%d\n",7/3);
10. printf("7%%3=%d\n",7%3);
11. printf("7.0/3.0=%f\n",7.0/3.0);
12. return 0;
13.}
Variables
• Variable Name for a memory object. Variable
names must start with letters and contain letters,
digits, and underscores.
• a, b, i, j, counter, number_of_points, c1234, …
• Type What the variable represents. Could be of
integer, floating point number, character, string,
and many others.
• int, float, double, char, …
• Declaration Tells compiler about variables and
their type.
• int i,j;
• float sum;
Numeric Types
int integer
float floating point
charater (a very shot
char
integer)
short or short
short integer
int
long or long int long integer
double long floating point
long double very long floating point
Reading Inputs
1. #include <stdio.h>
2. int main()
3. {
4. float a,b;
5. printf("Please input the first number:\n");
6. scanf("%f",&a);
7. printf("Please input the second number:\n");
8. scanf("%f",&b);

9. printf("a+b=%f\n",a+b);
10. printf("a-b=%f\n",a-b);
11. printf("a*b=%f\n",a*b);
12. printf("a/b=%f\n",a/b);
13.}
Assignment
1. /* Arithmetic operators */
2. #include <stdio.h>
3.
4. int main()
5. {
6. int a,c;
7. int b=4;
8. a=3;
9. c=a+b;
10. printf(“Sum: %d + %d -> %d\n”,a,b,c);
11. a++;b--;
12. prinf(“Now a=%d and b=%d\n”,a,b);
13. return 0;
14.}
In-Class Exercise 1-2
• Write a program to calculate the average
of three floating point numbers, namely
2.3, 3.4, 4.5.
• Using scanf function
• Using assignment

You might also like