0% found this document useful (0 votes)
26 views7 pages

Lab 2

The document provides information about a lab session on C programming and Octave. It includes examples of C programs with explanations and exercises. It also provides an introduction to using Octave for numerical computation and its basic functions.

Uploaded by

modamonika1985
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)
26 views7 pages

Lab 2

The document provides information about a lab session on C programming and Octave. It includes examples of C programs with explanations and exercises. It also provides an introduction to using Octave for numerical computation and its basic functions.

Uploaded by

modamonika1985
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/ 7

The Open University of Sri Lanka

Department of Electrical and Computer Engineering


Bachelor of Technology Honors in Engineering – Level 3
EEX 3417 – Software Development for Engineers
Academic Year 2019/2020
LAB 02
There are two sections for the LAB 02. First session – octave practical guide and Second Session –
C programming.
First Session – C Programming
Learning outcomes -At the end this practical session, student should be able to
1. Identify symbols of flow chart with loops
2. Describe how to program with loops and control structures
3. Describe how to program with character data type
4. Describe how to program with functions

Example 1: program to print from end to beginning (backward) which is entered 10 integer values
by you and to print the average value of the 10 integer values.

figure 1
Flow chart

(a)program (b)Description
void main() sum=sum+a[i]; - Take the sum of 10 values
{ of integer array. Add currently available
// declare variables value of sum with value of a[i] array
int i=0; element and assign it to the sum. For
int a[10]; example
int sum=0; if sum=1 and a=2
int N=9; sum=1+2=3.
float average=0; for(i=0;i<=N;i++) - for loop. “i++” is
for(i=0;i<=N;i++) increment (add 1 to existing value of “i”).
{ i<=N – condition (“i less than or equal to N)
printf("Enter %d integer number ",i); for(i=N;i>=0;i--) - for loop. “i--” is
scanf("%d",&a[i]); decrement (subtract 1 to existing value of
} “i”).
for(i=N;i>=0;i--) i>=0 – condition (“i greater than or equal to
{ 0)
printf("%d\n",a[i]); “printf("Average = %.2f ",average); ” is
sum=sum+a[i]; used to print the output. Here, %.2f means,
1
} variable “average” is a float real number
average=sum/(float)N; with 2 decimal points.
printf("Average = %.2f\n",average);
}
Figure 2

When you run the above program, “Enter 0 integer number” will display in the command prompt.
You can to enter 10 (0-9) any integer numbers.

Input Output
Enter 0 integer number 34 2
Enter 1 integer number 12 4
Enter 2 integer number 54 6
Enter 3 integer number 56 5
Enter 4 integer number 2 8
Enter 5 integer number 8 2
Enter 6 integer number 5 56
Enter 7 integer number 6 54
Enter 8 integer number 4 12
Enter 9 integer number 2 34
Average = 20.33
Output is a real number (not an integer
number). Therefore, variable average
in the program must be defined as
float.

Exercise 1: Explain the flow chart given in figure 1.


Exercise 2: Write a program that prints the following patterns. Use for loops to generate the
patterns. All asterisks ( * ) should be printed by a single printf statement of the form printf( "*" ).

*
**
***
****
*****
******
*******
********
*********
**********
***********

Example 02: Write a program to find the frequency of a character in a set of entered characters to
the computer. C code for the example 02 is given in figure 3. Description of lines of the program
which are difficult to understand is given in figure 4.

no Program code
1 #include <stdio.h>
2

2
3 int main()
4 {
5 char str[1000], ch;
6 int i, frequency = 0;
7
8 printf("Enter a string: ");
9 gets(str);
10
11 printf("Enter a character to find the frequency: ");
12 scanf("%c",&ch);
13
14 for(i = 0; str[i] != '\0'; ++i)
15 {
16 if(ch == str[i])
17 ++frequency;
18 }
19
20 printf("Frequency of %c = %d\n", ch, frequency);
21
22 return 0;
23 }
Figure 3

Line no Description
5 defines 2 variables called str[1000] and ch. “str[1000]” is a character array length of
1000 and “ch” is a character variable called “ch”. Difference between two char
variables are 1000 characters can be entered to first variable and only one character can
be entered to second variable.
9 Series of characters can be entered using “gets()” command
12 “c” of the "%c" is the data type of the character “ch”. It is character data type.
14 Beginning of the for loop to count number of characters in “str[1000]” array.
“str[i] != '\0'” indicates until the “str” array not equal to the enter key. “!=” means not
equal operator. When you press the “enter key” after entering required character string
the symbol '\0' will be inserted to the character array element.
17 ++ indicates the incremental operator.
Figure 4

Exercise 03: Change the above program to find number of spaces in a set of entered characters to
the computer.

Example 03: Write a program to input first name and last name separately into two character
variables and then combine two names with a space between names and assign to another character
variable called “fulname”. Then display the full name. C program code for the above example is
given in figure 5.

1 #include <stdio.h>
2 #include <string.h>
3
3 int main()
4 {
5 char fname[100];
6 char lname[100];
7 char fulname[200];
8 printf("Enter first name \n");
9 scanf("%s",fname);
10 printf("Enter last name \n");
11 scanf("%s",lname);
12 strcpy(fulname,fname);
13 strcat(fulname," ");
14 strcat(fulname,lname);
15 printf("Full Name %s \n",fulname);
16 }
Figure 5

Description of line of the program which are difficult to understand is given in figure 6.
1 #include <string.h> - header file needed to execute string functions such as “strcpy, strcat
12 Copy the string in fname to fulname
13 Add a space to the fulname
14 Add lname to the fulname
Figure 6

Example 04: Write a function to input integer value and return the square value. And write a
program to print square values of 1 to 10 integers.

1 #include <stdio.h>
2 int squre(int y);
3 int main(void)
4 {
5 int n;
6 for(n=1;n<=10;n++)
7 {
8 printf("%d ",squre(n));
9 }
10 printf("\n");
11 }
12 int squre(int y)
13 {
14 return y*y;
15 }

Exercise 04: Write a function of a C program to find power of a number. Take number as “x” and
power as y and name of the function is “power”. And then write a program to input value of “x”
and find the value of following equation.
value= 2∗ x 4+3∗ x 3 +4∗ x2 +x

4
Second Session - Octave practical guide
Octave is an open source mathematical software system for solving most complex
mathematical problems. Today Octave is widely used by engineers and scientists in industry and
academic to process complex numerical computations quickly.

Command line interface

User can type necessary commands needed to obtain the result at the command prompt. Command
prompt “>>” can be seen in the figure 1.

fig. 1 Octave command prompt

Numerical and array variables


Octave as calculator

Addition of 2 constants and addition of 2 variables are shown in figure 2.


[1] Type 2+2 at the command prompt. You can see the result as “ans=4”.
[2] Type a=2. That is declare variable “a” and assign 2 to the a.
[3] Type b=3.
[4] Type a+b. Result will be 5.

Creating vectors
A vector has number of columns and rows. Columns are separated by comma (,) and rows are
separated by semicolon (;).
Crate a vector with 3 columns and 1 row

Type 𝑎 = [1,3,2]at command prompt to create vector with 1 row and 3 columns as shown in figure
3.

fig 3. Create
vector with one
row

5
If you type array and semicolon at the end, result will not displayed as shown in figure 4. If you
type “a” again result will displayed.
𝑎 = [1,3,2];

fig 4. Use ; for not


displaying the result

Create vector with more than one row

Type 𝑎 = [1,3,2; 3,2,1; 1,2,3] to create vector

1,3,2
𝑎 = [3,2,1]
1,2,3

fig 5. create vector with 3


columns and 3 rows
Build in functions

There are functions shipped with octave. You can call function by typing function name and typing
relevant variables within brackets “( )”. “exp” of the following example is the function to find
exponential of a number. Here the number is “2”.
Type the following function “exp” and variable “2” at command prompt of the octave command
window and press enter. You can see the relevant output as “ans = 7.3891”

>> exp(2)
ans = 7.3891
Creating and editing a script

If you want to run several commands to get the output, you can save all necessary
commands in one text file and run the file in command line.
Then all the commands will fig 6. command and editor window run. Such a file is called
“script”. Script files can be select created by any text editor.
Extension of the file is .m.

First click on editor as shown in figure 6. Then type the code to add two variables called a and b and
assign addition to the variable called “s”. Then display the result. Here, a=2 and b=3. The code for
this program is shown in figure 7.
6
fig 7. add and
display result
To run the code in figure 7, click on command window and type the name of the file at the
command prompt.

Working on control structures

Flow of the program can be controlled by using control structures. There are 2 types of
control structures. They are -If then else and loop.

If then else

According to the program in figure user should input values


for the a and b variables. If a is equal to the b (a==b) then
multiply a and b “s=a*b” else “s=a+b”.

Fig 8. Program to
demonstrate if in octve

Loop

Figure 9 shows example program for “for loop”.

fig 9. example for


loop

You might also like