Review of scaler variables

Download Report

Transcript Review of scaler variables

Review of scalar variables
Variable (up to now) have:
name
type (int, float, double, char)
address
value
e.g.
N
35
28C4 (int)
q
3.14
28C8 (float)
r
8.9
28CC (float)
Name
type
address
value
N
integer
28C4
35
q
float
28C8
3.14
r
float
28CC
8.9
Intro to Arrays
Any single element of x may be used like any
other scalar variable
printf("%d",x[3]);
// prints 85
printf("%d",x[7]+x[1]); // prints 115
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
45 3044
55 3048
25 304C
85 3050
75 3054
65 3058
100 305C
60 3060
Intro to Arrays
Arrays declared by:
// Allocate 8 elements for x
int x[8];
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
45 3044
55 3048
25 304C
85 3050
75 3054
65 3058
100 305C
60 3060
Declaring Arrays
Define an 8 element array:
int x[8];
Elements numbered 0 to 7
Arrays in C always start
with 0 (zero based)
The initial value of each
array element is unknown
(just like scalar variables)
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
? 3044
? 3048
? 304C
? 3050
? 3054
? 3058
? 305C
? 3060
Declaring/defining Arrays
double A[]={ 1.23, 3.14159, 2.718, 0.7071 };
You can also define the
values to be held in the
array and instruct the
compiler to figure out how
many elements are needed.
Not putting a value within
the [] tells the compiler to
determine how many
locations are needed.
A[0]
A[1]
A[2]
A[3]
1.23
3.14159
2.718
0.7071
4430
4438
4440
4448
Working with Arrays (input)
#include <stdio.h>
void main(void)
{
int x[8];
int i;
float sum, avg; // used later
// get 8 values into x[]
for (i=0; i<8; i++)
{
printf("Enter test %d:",i+1);
scanf("%d",&x[i]);
}
Working with Arrays (input)
Sample run
(user input underlined):
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
test
test
test
test
test
test
test
test
1:80
2:75
3:90
4:100
5:65
6:88
7:40
8:90
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
80
75
90
100
65
88
40
90
Working with Arrays (find biggest)
// WAY 1-initialize biggest one so
// far to zero
big=0;
for (i=0; i<8; i++)
{
// if x[i] > biggest one so far
if (x[i]>big)
{
// make that the "new" biggest
big=x[i];
}
}
printf("Biggest is %d\n",big);
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
80
75
90
100
65
88
40
90
??? What happens if there are only negative values in the array ???
Working with Arrays (find biggest)
// WAY 2-initialize biggest one so
// far to smallest possible value
big= -2147483648;
for (i=0; i<8; i++)
{
// if x[i] > biggest one so far
if (x[i]>big)
{
// make that the "new" biggest
big=x[i];
}
}
printf("Biggest is %d\n",big);
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
???What happens if program is ported to another system???
??? (where integers are stored in 2 or 8 bytes)???
80
75
90
100
65
88
40
90
Working with Arrays (find biggest)
// WAY 3-initialize biggest one so
// far to smallest possible value
big= INT_MIN; // BUT use symbol
for (i=0; i<8; i++)
{
// if x[i] > biggest one so far
if (x[i]>big)
{
// make that the "new" biggest
big=x[i];
}
}
printf("Biggest is %d\n",big);
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
80
75
90
100
65
88
40
90
The above works, but there is one more (slightly better) way
Working with Arrays (find biggest)
// WAY 4-initialize biggest one so
// far to the first array element
big= x[0];
for (i=1; i<8; i++) // start at 1
{
// if x[i] > biggest one so far
if (x[i]>big)
{
// make that the "new" biggest
big=x[i];
}
}
printf("Biggest is %d\n",big);
-----
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
80
75
90
100
65
88
40
90
Working with Arrays (find smallest)
// initialize smallest one so
// far to the first array element
sml= x[0];
for (i=1; i<8; i++)
{
// if x[i] < smallest one so far
if (x[i]<sml)
{
// make that the "new" smallest
sml=x[i];
}
}
printf("Smallest is %d\n",sml);
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
80
75
90
100
65
88
40
90
Ways 1, 2, and 3 for finding biggest could be adapted to find smallest
as well; the above is the one usually used.
Working with Arrays (find average)
// initialize sum
// for reference:
//
int sum;
//
float avg;
sum = 0;
// add each element of x[] to sum
for (i=0; i<8; i++)
sum += x[i];
// divide by n (8) to get average
// first float needed since result
// datatype of division is a double
avg=(float)((float)sum/8.0);
printf("Average is %.2f\n",avg);
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
80
75
90
100
65
88
40
90
Passing Arrays to functions (findBig)
//*******************************************
// function findBig
// On Entry:
//
test[] - array with values
//
n
- number of elements to examine
// On Exit:
//
returns biggest value in the first n
//
elements of test[]
int findBig(int test[], int n)
{
int i,big;
big=test[0];
for (i=1; i<n; i++)
if (x[i]>big) big=x[i];
return big;
}
Passing Arrays to functions (findSml)
//*******************************************
// function findSml
// On Entry:
//
test[] - array with values
//
n
- number of elements to examine
// On Exit:
//
returns smallest value in the first n
//
elements of test[]
int findSml(int test[], int n)
{
int i,sml;
sml=test[0];
for (i=1; i<n; i++)
if (x[i]<sml) sml=x[i];
return sml;
}
Passing Arrays to functions (findAvg)
//*******************************************
// function findAvg
// On Entry:
//
test[] - array with values to avg
//
n
- number of values to avg
// On Exit:
//
returns avg of first n elements of test[]
float findAvg(int test[], int n)
{
int i,sum=0;
float avg;
for (i=0; i<n; i++)
sum+=test[i];
avg=(float)((float)sum/(float)n);
return avg;
}
Passing Arrays to functions (SclAry)
//*******************************************
// function SclAry
// On Entry:
//
test[] - array with values to scale
//
n
- number of values to scale
//
p
- number of points to scale
// On Exit:
//
The first n values of test[] are
//
scaled by p points
void SclAry(int test[], int n, int p)
{
int i;
for (i=0; i<n; i++)
test[i]=test[i]+p; // or use test[i]+=p;
}
Passing Arrays to functions (SclAry)
#include <stdio.h>
void SclAry(int test[], int n, int p);
void main(void)
{ int i;
int x[]={ 51,62,73,84,95,100,66,57,48,79 };
int N=sizeof(x)/sizeof(int);
SclAry(x,N,10);
for (i=0; i<N; i++)
printf("%4d",x[i]);
printf("\n");
}
void SclAry(int test[], int n, int p)
{
int i;
for (i=0; i<n; i++)
test[i]=test[i]+p; // or use test[i]+=p;
}
Passing Arrays to functions (SclAry)
Output of program:
61
72
83
94 105 110
76
67
58
89
For reference:
int x[]={ 51,62,73,84,95,100,66,57,48,79 };
??? What's wrong with this picture ???
Passing Arrays to functions (SclAry)
Output of program:
61
72
83
94 105 110
76
67
58
89
For reference:
int x[]={ 51,62,73,84,95,100,66,57,48,79 };
??? What's wrong with this picture ???
The array in the main program was UPDATED ... (say "Hmmmm")
Does this seem contrary to all we know about functions? (say "Yes")
Is this how it really works? (Yep, it is)
Is your head getting ready to explode? (say "Almost")
??? SO WHAT IS GOING ON ???
Passing Arrays to functions
Before call to SclAry
After call to SclAry
test[0]
51 3044
test[0]
61 3044
test[1]
62 3048
test[1]
72 3048
test[2]
73 304C
test[2]
83 304C
test[3]
84 3050
test[3]
94 3050
test[4]
95 3054
test[4]
105 3054
test[5]
100 3058
test[5]
110 3058
test[6]
66 305C
test[6]
76 305C
test[7]
57 3060
test[7]
67 3060
test[8]
48 3064
test[8]
58 3064
test[9]
79 3068
test[9]
89 3068
Passing the name only (i.e. test vs. test[4]) passes the
ADDRESS of element zero of the array.
Put another way:
myfunc(ary) same as myfunc (&ary[0])
Sorting Arrays (prelim - swapping)
Before
x[0]
61
x[1]
172
x[2]
83
x[3]
74
x[4]
99
x[5]
11
t
83 ?
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
61
172
83
74
99
11
After
x[0]
61
x[1]
172
x[2]
74
x[3]
83
x[4]
99
x[5]
11
int i,j,t;
j=2;
t=x[j];
// 1
x[j]=x[j+1]; // 2
x[j+1]=t;
// 3
t
1
83
x[0]
61
x[1]
172
x[2] 74 83
x[3]
74
x[4]
99
x[5]
11
t
2
83
x[0]
61
x[1]
172
x[2] 74 83
x[3] 83 74
x[4]
99
x[5]
11
3
Sorting Arrays
void bubblesort(int x[], int n)
{
int i,j,t;
for (i=0; i<n; i++)
{
for (j=0; j<(n-1); j++)
{
if (x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
}
void bubblesort(int x[], int n)
int i,j,t;
for (i=0; i<n; i++)
for (j=0; j<(n-1); j++)
if (x[j]>x[j+1])
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
// say n=6
Sorting Arrays
Pass 0
i=0,j=4
61
83
74
99
11 172
172 11
172>11? Yes, swap
i=0,j=3
61
83
74
99 172
172 99
11
172>105? Yes, swap
i=0,j=2
61
83
74 172
172 74
99
11
172>74? Yes, swap
i=0,j=1
61
83 172
172 83
74
99
11
172>83? Yes, swap
61>172? No, no swap
i=0,j=0
x[0]
61
x[1]
172
x[2]
83
x[3]
74
x[4]
99
x[5]
11
void bubblesort(int x[], int n)
int i,j,t;
for (i=0; i<n; i++)
for (j=0; j<(n-1); j++)
if (x[j]>x[j+1])
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
// say n=6
Sorting Arrays
Pass 1
i=1,j=4
61
83
74
11
99
172
99>172? No, no swap
i=1,j=3
61
83
83
11 99
99 11
172
99>11? Yes, swap
i=1,j=2
61
74
83
99
11
172
83>99? No, no swap
i=1,j=1
61
74 83
83 74
99
11
172
83>74? Yes, swap
61>83? No, no swap
i=1,j=0
x[0]
61
x[1]
83
x[2]
74
x[3]
99
x[4]
11
x[5]
172
void bubblesort(int x[], int n)
int i,j,t;
for (i=0; i<n; i++)
for (j=0; j<(n-1); j++)
if (x[j]>x[j+1])
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
// say n=6
Sorting Arrays
Pass 2
i=2,j=4
61
74
11
83
99
172
99>172? No, no swap
i=2,j=3
61
74
11
83
99
172
83>99? No, no swap
i=2,j=2
61
74
11 83
83 11
99
172
83>11? Yes, swap
i=2,j=1
61
74 83
83 74
11
99
172
83>74? Yes, swap
61>83? No, no swap
i=2,j=0
x[0]
61
x[1]
83
x[2]
74
x[3]
11
x[4]
99
x[5]
172
void bubblesort(int x[], int n)
int i,j,t;
for (i=0; i<n; i++)
for (j=0; j<(n-1); j++)
if (x[j]>x[j+1])
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
// say n=6
Sorting Arrays
Pass 3
i=3,j=4
61
11
74
83
99
172
99>172? No, no swap
i=3,j=3
61
11
74
83
99
172
83>99? No, no swap
i=3,j=2
61
11
74
83
99
172
74>83? No, no swap
i=3,j=1
61
11 74
74 11
83
99
172
74>11? Yes, swap
61>74? No, no swap
i=3,j=0
x[0]
61
x[1]
74
x[2]
11
x[3]
83
x[4]
99
x[5]
172
void bubblesort(int x[], int n)
int i,j,t;
for (i=0; i<n; i++)
for (j=0; j<(n-1); j++)
if (x[j]>x[j+1])
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
// say n=6
Sorting Arrays
Pass 4
i=4,j=4
11
61
74
83
99
172
99>172? No, no swap
i=4,j=3
11
61
74
83
99
172
83>99? No, no swap
i=4,j=2
11
61
74
83
99
172
74>83? No, no swap
i=4,j=1
11
61
74
83
99
172
61>74? No, no swap
61>11? Yes, swap
i=4,j=0
x[0] 11 61
x[1] 61 11
x[2]
74
x[3]
83
x[4]
99
x[5]
172
void bubblesort(int x[], int n)
int i,j,t;
for (i=0; i<n; i++)
for (j=0; j<(n-1); j++)
if (x[j]>x[j+1])
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
// say n=6
Sorting Arrays
Pass 5
i=5,j=4
11
61
74
83
99
172
99>172? No, no swap
i=5,j=3
11
61
74
83
99
172
83>99? No, no swap
i=5,j=2
11
61
74
83
99
172
74>83? No, no swap
i=5,j=1
11
61
74
83
99
172
61>74? No, no swap
11>61? No, no swap
i=5,j=0
x[0]
11
x[1]
61
x[2]
74
x[3]
83
x[4]
99
x[5]
172
Sorting Arrays Analysis
Before
i=0,j=0
x[0]
61
x[1]
172
x[2]
83
x[3]
74
x[4]
99
x[5]
11
After After
After
After After
After
Pass 0 Pass 1 Pass 2 Pass 3 Pass 4 Pass 5
i=0,j=4 i=1,j=4 i=2,j=4 i=3,j=4 i=4,j=4 i=5,j=4
61
61
61
61
11
11
83
83
74
11
61
61
74
74
11
74
74
74
99
11
83
83
83
83
11
99
99
99
99
99
172
172
172
172
172
172
Biggest number in
correct position
Biggest two numbers in correct position
Biggest three numbers in correct position
Biggest four numbers in correct position
Biggest five numbers
in correct position (so
really all six numbers
in correct position).
Pass 5 is really a waste.
Analysis (n=100, array in rev order)
i=0;
while (i<n)
j=0;
while (j<(n-1))
if (x[j]>x[j+1])
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
j++;
i++;
#exec #cyc time(ns)
1 1
1
100 1
100
100 1
100
9900 2 19800
9900 5 49500
4950 3 14850
4950 5 24750
4950 4 19800
9900 1
9900
100 1
100
44851
138901
Analysis (General for n)
i=0;
1
while (i<n)
100
j=0;
100
while (j<(n-1))
9900
if (x[j]>x[j+1])9900
t=x[j];
4950
x[j]=x[j+1]; 4950
x[j+1]=t;
4950
j++;
9900
i++;
100
1
n
n
n*(n-1)
n*(n-1)
(n*(n-1))/2
(n*(n-1))/2
(n*(n-1))/2
n*(n-1)
n
Analysis (General for n)
#ex
i=0
1
while(i<n)
n
j=0
n
while (j<(n-1))
n*(n-1)
if (x[j]>x[j+1]) n*(n-1)
t=x[j]
n*(n-1)/2
x[j]=x[j+1]
n*(n-1)/2
x[j+1]=t
n*(n-1)/2
j++
n*(n-1)
i++
n
#cy
1
1
1
2
5
3
5
4
1
1
t (ns)
1
n
n
2*n*(n-1)
5*n*(n-1)
3/2*n*(n-1)
5/2*n*(n-1)
4/2*n*(n-1)
n*(n-1)
n
Analysis (General for n)
t = 1 + 3n + 14[n(n-1)]
t = 1 + 3n + 14n2 - 14n
t = 14n2 - 11n + 1
Check for n=100:
t = 14(100)2 - 11(100) + 1
t = 140000 - 1100 + 1
t = 138901 ns = 138.9 us = 0.138 ms
Analysis (General for n)
n
t (ms) on 1 GHz
t/n
100
0.139
0.00139
1000
13.989
0.013989
10000
1399.89
0.139989
100000
139998.90
1.399989
1000000
13999989.00
13.999989
This is a linear function for n
Sorting
Arrays
bubblesort(int x[], int n)
void
{
int i,j,t,n1;
n1=n-1;
for (i=0; i<n1; i++)
{
for (j=0; j<(n1-i); j++)
{
if (x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
}
}
Sorting
Arrays
bubblesort(int x[], int n)
void
{
int i,j,t,n1, F=1;
n1=n-1;
for (i=0; i<n1; i++)
{ if (F) { F=0;
for (j=0; j<(n1-i); j++)
{
if (x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;F=1;
}
}
}}
}
Sorting
Arrays
bubblesort(int x[], int n)
void
{
int i,j,t,n1, F=1;
n1=n-1;
for (i=0; (i<n1)&&F; i++)
{ F=0;
for (j=0; j<(n1-i); j++)
{
if (x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;F=1;
}
}
}}
}