Anda di halaman 1dari 23

Chapter contains

• Single dimensional array


Arrays • Multiple dimensional array
Dr Ho Fu Haw • Pointer

Function of array: to store huge data


sets in a systematic way

• Normally we stored value as variable a,b,c..


• How about if we have 1000 variables ?
• Array- A set of data with similar properties and
which are stored in consecutive memory location
under common variable name
HOW TO DECLARE ARRAY??
HOW TO DECLARE ARRAY?

Integer and character array One dimensional array

• int x[10]
• Array is declared as ordinary variable, but size of the
array must be specified within the square bracket
• int main()
{ int number[5];
Exercise int i;
long sum;
1. Develop a program to store X1 until X5 data, and int average ;
continue to calculate the average value X1 until X5 int count = 5;
without using array function.
for (i=0; i<count; i ++)
{ printf("enter value %d", i+1);
2. By using array function, develop a program to scanf("%d", &number[i]);
evaluate X1 until X5, and calculate the average of
total obtained value. sum += number[i]; }
average = sum/5;
printf("average of five numbers is: %d \n", average);}

• // Program to find the average of n (n < 10) numbers using arrays


• #include <stdio.h>
• int main()
• {
• int x[5], i, n, sum = 0, average;
• printf("Enter n: ");
• scanf("%d", &n);
• for(i=0; i<n; ++i)
• { printf("Enter number%d: ",i+1);
• scanf("%d", &x[i]);
• sum += x[i]; }
• average = sum/n;
• printf("Average = %d", average);
• return 0;}
• #include<stdio.h>

• int main()
• {
Write a program to store 5 digits below and perform
• int mark[4] = {12,10,13,5};
different arithmetic operation on these digits using
array function: • printf("first operation: %d \n",mark[0]-mark[2]);
• printf("second operation: %d\n",mark[1]*mark[3]);
4 5 11 12 9
• printf("third operation: %d \n",mark[2]%mark[4]);
First operation: 4/5
Second operation: 4+11/12
• return 0;
Third operation: 9%11 •
• }

TRY THIS?
Retrieving stored data
DEVELOP A PROGRAM TO SHOW STUDENT’S
For (int i= 0; i< count; i=i+1) TEST MARK WHEN THEIR MATRIC NO IS
Printf(“ the number %d was %d”, i+1, number[i]); TYPED USING ARRAY FUCNTION. THE
STUDENT AND THEIR MARKS ARE AS BELOW:
MATRIC NO TEST MARK
111 10
222 20
333 30
444 40
555 50
• #include<stdio.h>
• int main()
• {
• int mat[]={111,222,333,444,555}, i;
• int arr[] = {10,20,30,40,50};
• Printf(“enter your matric number: “,i);
• Scanf(“%d”, &i);
• mat[i]=arr[i];
• printf("matrix number of %d is %d \n", mat[i], arr[i]);

• return 0;
• }

Example of one dimensional array


Subscript
• Array also known as subscripted variable
• General form
• Name[i], number [i][j]
• i and j is subscript
• Following rules should be followed while using subscript
• A subscript may be a valid integer
• The value of subscript is zero or positive
• A subscript must not be subscripted variable
• If a variable is used to represent an array it should not be used as
ordinary variable
Run time initialization

• Array can be initialize during execution of the


program in 2 different ways
• By using assignment statement
• By using input statement

Exercise Multidimensional array

• • In multidimensional array the number of subscript is more than


1
• int a[i][j][k]
• The declaration is similar to one dimensional array.
• int x[3][2]
• x[0][0] x[0][1]
• x[1][0] x[1][1]
• x[2][0] x[2][1]
• The size of this array is 6
• #include<stdio.h>
• #include<conio.h>
• int main()
• { int x[3][3]={1,2,3,[0]=4,5,6,7,8,9};
printf("\n%d", x[1][2]);
• return 0; }
• #include<stdio.h>
• #include<conio.h>
• int main()
Exercise • { int x[2][2]={1,2,3,4}, i, j;
• printf("enter your x point: ");
• scanf("%d", &i);
• printf("enter your y point: ");
• scanf("%d", &j);
• printf("the coordinate x[]y[] is: %d", x[i][j]);
Write a program to show the • return 0;
• }
coordinate of [i][j] by key in i & j
in a any 3x3 matrix.
Write a program to find the type of
vegetable and its quantity:
Location Vegetable Qty
[0][0] tomato 5
[1][1] kangkung 4
[1][2] Broccoli 3

President of United State


Exercise

• Write a program to find the transpose matrix of


order m x n;
Pointer
ƒ When declaring a variable, the compiler sets aside memory
storage with a unique address to store that variable.
ƒ The compiler associates that address with the variable’s name.
ƒ When the program uses the variable name, it automatically
accesses a proper memory location.
ƒ No need to concern which address.
ƒ But we can manipulate the memory address by using pointers.
ƒ Let say we declare a variable named nRate of type integer and
assign an initial value as follows,

int nRate = 10;

• A pointer is a variable that stores the address of


another variable. Unlike other variables that hold
advantages values of a certain type, pointer holds the address of
a variable.

• By using pointers, it is an efficient way of accessing


and manipulating data.
• Very useful for dynamic memory management, as we
know memory, registers and cache are scarce
resource in computer system.
• Reduce running time
Define pointer Pointer outputs
• “Address of ”(&) Operator – to find the address of a defined
identifier
Example: printf(“the address of num is: %p”, &num)

“Value at Address”(*) Operator- to define an empty pointer


Example: int *p

“%p, %x, %X” – to print pointer

*p in printf – to retrieve the value contained in a pointer


Example: printf(“the value of num is %p”, *p)

Difference between %p and %x


Type of pointer

• "x" and "X" serve to output a hexadecimal number. "x" stands


for lower case letters (abcdef) while "X" for capital letters
(ABCDEF).
• "p" serves to output a pointer. It may differ depending upon
the compiler and platform.

Example:
Printf(“the address of ABC is: %p”, &abc);
Printf(“the address of ABC is: %x”, &abc);
Printf(“the address of ABC is: %X”, &abc);
Pointer arithmetic (++, -- , + -)
Arithmetic on pointer variable

• Arithmetic operation such as +,-,++ and – can be


performed on pointer variable under some restriction
• Depends on size of data type
• Character – 1byte
• Int- 4 bytes
• Float- 4 bytes
• Double- 8 bytes
• If float *a and a++ the size increase by 4

Decrement pointer
Pointer and One dimensional Array

• Address of the first element of array is address of


entire array
• a[] Æ a is an integer of array, then the address of
first element either &a[0] or by simply a.
• If Pa is pointer define by int *Pa and array a defined
by int a[];
• Pa=&a[0]; or Pa=a; is same
• Pa=&a[1] is same with Pa=a+1;
Array of pointer – store array data in use an array of pointers to character
pointer to store a list of strings

Example of pointer to pointer:


Pointer to pointer

• A pointer to a pointer is a form of multiple indirection, or


a chain of pointers.

• declares a pointer to a pointer of type int :


Example: int **num, ***num1, ****num2;
Dynamic memory allocation
• Int a[100]
• Fixed amount of memory is reserved for array
• 100 x 4 bytes of memory reserved for a. in this case if
only 40 is used 60 space is unnecessarily occupied. This is
wastage of memory.
• Dynamic memory allocation is useful function to
overcome this problem
• Library function:
• malloc
Exercise
1.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x,y;
printf("Please insert x value\n");

scanf("%d",&x);
if(x < 5 && x >=0)
{
y=5*x+12;
printf("The y value is :%d", y);
}
else if (x < 15 && x>=6)
{
y=0.5*x-12;
printf("The y value is :%d", y);
}
else if (x >= 20 && x<30)
{
y=pow(12-12*x,3);
printf("The y value is :%d", y);
}
else
{
printf("\nOut of range");
}
getchar();
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++
2.
#include<stdio.h>
#include<math.h>
int main()
{
int i,n,x,y[9999],a=0,b;
printf("Please insert the n value\n");
scanf("%d",&n);
for(x=1;x<=n;x++)
{
i=2*x+5;
printf("Y = %d\n",i);
y[x-1]=i;
}
for ( x = 0; x < n; x++)
{
a+=y[x];
}
printf("The average value is %d",a/n);
getchar();
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++
3.
#include<stdio.h>
int main()
{
int *a,i,j,tmp,n;
printf("\n\nPointer : Sort an array using pointer:\n");
printf("Input the number element to store in array:");
scanf("%d",&n);
printf("Input %d number element to store in array:",n);
for(i=0 ; i<n ; i++)
{
printf("element - %d:",i+1);
scanf("%d",&a+1);
}
for(i=0 ; i<n ; i++)
{
for(j=i+1 ; j<n ; i++)
{
if(*(a+i)>*(a+j))
{
tmp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=tmp;
}
}
} printf("\n The elements in the array after sorting : \n");
for(i=0 ; i<n ; i++)
{
printf("element - %d:%d",i+1,*(a+i));
printf("\n");
}
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++
4.
ARRAY

EG1.

Develop a program to store X1 until X5 data, and


continue to calculate the average value X1 until X5
without using array function.

int main()
{
int number[5];
int i;
long sum;
int average ;
int count = 5;
for (i=0; i<count; i ++)
{
printf("enter value %d", i+1);
scanf("%d", &number[i]);
sum += number[i];
}
average = sum/5;
printf("average of five numbers is: %d \n", average);}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EG2.

By using array function, develop a program to


evaluate X1 until X5, and calculate the average of
total obtained value.

// Program to find the average of n (n < 10) numbers using arrays


#include <stdio.h>
int main()
{
int x[5], i, n, sum = 0, average;
printf("Enter n: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
printf("Enter number%d: ",i+1);
scanf("%d", &x[i]);
sum += x[i];
}
average = sum/n;
printf("Average = %d", average);
return 0;
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

EG3.

Write a program to show the


coordinate of [i][j] by key in i & j
in a any 3x3 matrix.

#include<stdio.h>
#include<conio.h>
int main()
{
int x[2][2]={1,2,3,4}, i, j;
printf("enter your x point: ");
scanf("%d", &i);
printf("enter your y point: ");
scanf("%d", &j);
printf("the coordinate x[]y[] is: %d", x[i][j]);
return 0;
}

______(OUTPUT)__________
enter your x point: 1
enter your y point: 2
the coordinate x[]y[] is: 43979632
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

5. LOOPS

EG1

FOR LOOP (SUM)

// Program to calculate the sum of first n natural number


// Positive integer 1,2,3,n... are known as natural numbers

#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);

for (count = 1; count <= num; ++count )


{
sum += count;
{
printf("Sum = %d", sum);
return 0;
}

________(OUTPUT)__________
Enter a positive integer: 10
Sum = 55

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EG2

WHILE LOOP

#include<stdio.h>
int main()
{
int i = 10;
printf("enter a number", i);
scanf ("%d", &i);

while(i>0)
{
printf("Hello %d\n",i);
i=i-1;
}
}

______OUTPUT______

enter a number8
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6. Switching

EG1
Write a program to enable the system to pick a number
between 1 to 10 and prize to be won as below:
no 1 ¨C Myvi
no 3 ¨C EX5 bike
no 7¨C Raleigh mountain bike
no 8 ¨C acer laptop
Else: mineral water

#include<stdio.h>
int main()
{
int number;
start:
printf("key in your number:",number);
scanf("%d", &number);

switch(number)
{
case 123:
printf("you won the 1st price");
break;

case 456:
printf("you won the 2nd price");
Break;

case 789:
printf("you won the 3rd price");
break;

default:
printf("too bad, you won nothing!!");
}
return 0;
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

EG2

Build a program to evaluate:


a < 5 goto formula1: y=10a + 5
a < 8 goto formula2: y= 25-2a
a > 8 goto formula3: y= 12a-4
If y value > 20, repeat the evaluation program, else end the
program.

#include <stdio.h>
#include <conio.h>
int main(void)
{
int a,b,c,y;

start:
printf("Enter a value: ");
scanf("%d",&a);
if(a<5)
{
goto formula1;
formula1:
printf("%d",y=10*a+5);
}

else if(a<8)
{
goto formula2;
formula2:
printf("%d",y=25-2*a);
}

else
{
goto formula3;
formula3:
printf("%d",y=12*a-4);
}

if(y>20)
goto start;

return 0;
The ASCII Code Table

Dec Hex Symbol Dec Hex Symbol Dec Hex Symbol Dec Hex Symbol
0 0x00 Null character 32 0x20 space 64 0x40 @ 96 0x60 `
1 0x01 Start of heading 33 0x21 ! 65 0x41 A 97 0x61 a
2 0x02 Start of text 34 0x22 " 66 0x42 B 98 0x62 b
3 0x03 End of text 35 0x23 # 67 0x43 C 99 0x63 c
4 0x04 End of transmission 36 0x24 $ 68 0x44 D 100 0x64 d
5 0x05 Enquiry 37 0x25 % 69 0x45 E 101 0x65 e
6 0x06 Acknowledgment 38 0x26 & 70 0x46 F 102 0x66 f
7 0x07 Bell 39 0x27 ' 71 0x47 G 103 0x67 g
8 0x08 Backspace 40 0x28 ( 72 0x48 H 104 0x68 h
9 0x09 Horizontal tab 41 0x29 ) 73 0x49 I 105 0x69 i
10 0x0A Line feed 42 0x2A * 74 0x4A J 106 0x6A j
11 0x0B Vertical tab 43 0x2B + 75 0x4B K 107 0x6B k
12 0x0C Form feed 44 0x2C , 76 0x4C L 108 0x6C l
13 0x0D Carriage return 45 0x2D - 77 0x4D M 109 0x6D m
14 0x0E Shift out 46 0x2E . 78 0x4E N 110 0x6E n
15 0x0F Shift in 47 0x2F / 79 0x4F O 111 0x6F o
16 0x10 Data link escape 48 0x30 0 80 0x50 P 112 0x70 p
17 0x11 Device Control 1 49 0x31 1 81 0x51 Q 113 0x71 q
18 0x12 Device Control 2 50 0x32 2 82 0x52 R 114 0x72 r
19 0x13 Device Control 3 51 0x33 3 83 0x53 S 115 0x73 s
20 0x14 Device Control 4 52 0x34 4 84 0x54 T 116 0x74 t
21 0x15 Negative Acknowledgment 53 0x35 5 85 0x55 U 117 0x75 u
22 0x16 Synchronous Idle 54 0x36 6 86 0x56 V 118 0x76 v
23 0x17 End of Transmission Block 55 0x37 7 87 0x57 W 119 0x77 w
24 0x18 Cancel 56 0x38 8 88 0x58 X 120 0x78 x
25 0x19 End of Medium 57 0x39 9 89 0x59 Y 121 0x79 y
26 0x1A Substitute 58 0x3A : 90 0x5A Z 122 0x7A z
27 0x1B Escape 59 0x3B ; 91 0x5B [ 123 0x7B {
28 0x1C File Separator 60 0x3C < 92 0x5C \ 124 0x7C |
29 0x1D Group Separator 61 0x3D = 93 0x5D ] 125 0x7D }
30 0x1E Record Separator 62 0x3E > 94 0x5E ^ 126 0x7E ~
31 0x1F Unit Separator 63 0x3F ? 95 0x5F _ 127 0x7F delete

Provided by c-programming-simple-steps.com – your portal for C programming

Anda mungkin juga menyukai