+ All Categories
Home > Documents > Address and Value of Array

Address and Value of Array

Date post: 01-Jan-2016
Category:
Upload: marigold-suarez
View: 22 times
Download: 0 times
Share this document with a friend
Description:
Address and Value of Array. int a[10]; declare array of 10 integer data items The address of the first memory cell of the first element is &a[0] or just a The address of the first memory cell of the i-th element is &a[i-1], or a+(i-1) - PowerPoint PPT Presentation
10
CP104 Introduction to Programming Array Lecture 23 __ 1 Address and Value of Array int a[10]; declare array of 10 integer data items The address of the first memory cell of the first element is &a[0] or just a The address of the first memory cell of the i- th element is &a[i-1], or a+(i-1) The name a is actually an address reference of the array int *addr; addr = a; define a pointer veriable addr and then assign the address of a[] to addr *(addr+2) = 1; /* assign value 1 to the third data item */
Transcript
Page 1: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 1

Address and Value of Array

• int a[10]; declare array of 10 integer data items

• The address of the first memory cell of the first element is &a[0] or just a

• The address of the first memory cell of the i-th element is &a[i-1], or a+(i-1)

• The name a is actually an address reference of the arrayint *addr; addr = a; define a pointer veriable addr and then assign the address of a[] to addr*(addr+2) = 1; /* assign value 1 to the third data item */

Page 2: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 2

Demo on Array Address

#include <stdio.h>

int main(){ int j;

int *addr, a[10] = {1, 2, 3, 4}; double b[10] = {1.0, 3.0, 3};

printf("%d\n", a); printf("%d\n", a[1]); printf("%d\n", &a);

printf("The address of array elements of a[]: \n"); for (j=0; j<10; j++) { printf("%d\n", &a[j]); }

printf("The address of array elements of b[]: \n"); for (j=0; j<10; j++) { printf("%d\n", &b[j]); }

printf("Pointer and array\n"); addr = a;

printf("%d\n", addr);

printf("%d\n", addr+2); *(addr+2) = 1; printf("%d\n", *(addr+2));

}

Page 3: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 3

Array and Function

• Array elements as function argumentsprintf(“%d”, a[0]);scanf(“%d”, &a[0]);

• Formal array argumentvoid fill_array(int list[], int n, int in_value) { int i; for (i = 0; i < n; ++i) list[i] = in_value;}

int y[10];fill_array(y, 10, 1); /* pass the address of the array */fill_array(&y[0], 10, 1);

To fill all array element of y by 1

Page 4: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 4

Data Areas Before Return from fill_array (x, 5, 1);

Page 5: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 5

Function to Find the Largest Element in an Array

Array as input argument

Int get_max(const int list[], int n) { int i, cur_large;

cur_large = list[0];

for (i = 1; i < n; ++i) if (list[i] > cur_large) cur_large = list[i];

return (cur_large);}Const is

Page 6: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 6

Function to Add Two Arrays

Page 7: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 7

Function Data Areas for add_arrays(x, y, x_plus_y, 5);

Page 8: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 8

Bubble Sort Function

#include <stdio.h>#include <stdlib.h>#include <time.h>#define MAX 50000

void bubble_sort(int a[]);

int main(){ int j, a[MAX]; /* clock_t ticks1, ticks2; */

for (j=0; j<MAX; ++j) { a[j] = rand()%100; printf("%d, ", a[j]); }

/* ticks1=clock(); */ bubble_sort(a);

/* ticks2=clock(); printf("Took %f ticks to wait one second.\

n", (double) (ticks2-ticks1)/CLOCKS_PER_SEC); */

printf("\n\nThe sorted sequence is:\n\n"); for (j = 0; j<MAX; ++j) printf("%d, ", a[j]);

fflush(stdin); getchar(); return 0;}

void bubble_sort(int a[]){ int i, j, t;

for (j = 0; j< MAX; j++) for (i = 0; i < MAX - 1 -j; i++) if (a[i] > a[i+1]) { t = a[i]; a[i] = a[i+1]; a[i+1] = t; } }

Page 9: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 9

Function Using a Sentinel-Controlled Loop to Store Input Data in an Array

Page 10: Address and Value of Array

CP104 Introduction to Programming Array Lecture 23 __ 10

Driver for Testing fill_to_sentinel


Recommended