Top Banner
19

Global variables, sorting static variables,function and arrays,

Jan 21, 2018

Download

Engineering

Ahmad55ali
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Global variables, sorting static variables,function and arrays,
Page 2: Global variables, sorting static variables,function and arrays,

GROUP # 04

GROUP MEMBERS;

HAFIZ AHMAD ALI EN-15024

SAQIB KHOKHAR EN-15017

HASNAIN BILAL EN-15025

Page 3: Global variables, sorting static variables,function and arrays,

GLOBAL VARIABLE

• GLOBAL VARIABLES HOLD THEIR VALUES THROUGHOUT THE LIFETIME OF YOUR PROGRAM AND THEY CAN BE ACCESSED INSIDE ANY OF THE FUNCTIONS DEFINED FOR THE PROGRAM.

• GLOBAL AND LOCAL VARIABLES

• A LOCAL VARIABLE IS A VARIABLE THAT IS DECLARED INSIDE A FUNCTION.

• A GLOBAL VARIABLE IS A VARIABLE THAT IS DECLARED OUTSIDE ALL FUNCTIONS.

• A LOCAL VARIABLE CAN ONLY BE USED IN THE FUNCTION WHERE IT IS DECLARED.

• A GLOBAL VARIABLE CAN BE USED IN ALL FUNCTIONS.

Page 4: Global variables, sorting static variables,function and arrays,

#include<stdio.h> // Global variables int A; int B; int Add() { return A + B; } int main() { int answer; // Local variable A = 5; B = 7; answer = Add(); printf("%d\n",answer); return 0; }

Page 5: Global variables, sorting static variables,function and arrays,

Sorting

To arrange a set of items in sequence.

Many applications require sorting;

Many applications perform sorting when they don't have to;

Many applications use inefficient sorting algorithms.

Page 6: Global variables, sorting static variables,function and arrays,

Sorting Applications

To prepare a list of student ID, names, and scores in a table (sorted by ID or name) for easy checking.

To prepare a list of scores before letter grade assignment.

To produce a list of horses after a race (sorted by the finishing times) for payoff calculation.

To prepare an originally unsorted array for ordered binary searching.

Page 7: Global variables, sorting static variables,function and arrays,
Page 8: Global variables, sorting static variables,function and arrays,
Page 9: Global variables, sorting static variables,function and arrays,

Static and Automatic Variables

• Automatic variable - memory is allocated at block entry and deallocated at block exit

• Static variable - memory remains allocated as long as the program executes

• Variables declared outside of any block are static variables

• By default, variables declared within a block are automatic variables

• Declare a static variable within a block by using the reserved word static

Page 10: Global variables, sorting static variables,function and arrays,

Static and Automatic Variables (continued)

• The syntax for declaring a static variable is:

static dataType identifier;

• The statement

static int x;

declares x to be a static variable of the type int

• Static variables declared within a block are local to the block

• Their scope is the same as any other local identifier of that block

Page 11: Global variables, sorting static variables,function and arrays,

Summary (continued)

• An automatic variable is a variable for which memory is allocated on function (or block) entry and deallocated on function (or block) exit

• A static variable is a variable for which memory remains allocated throughout the execution of the program

• C allows functions to have default parameters

Page 12: Global variables, sorting static variables,function and arrays,

C FUNCTIONS

Basically there are two categories of function:

1. Predefined functions: available in C / C++

standard library such as stdio.h, math.h,

string.h etc.

2. User-defined functions: functions that

programmers create for specialized tasks such

as graphic and multimedia libraries,

implementation extensions or dependent etc.

Page 13: Global variables, sorting static variables,function and arrays,

FUNCTION & ARRAY:

FOR A FUNCTION TO RECEIVE AN ARRAY THROUGH A FUNCTION CALL, THE

FUNCTION’S PARAMETER LIST MUST SPECIFY THAT AN ARRAY WILL BE RECEIVED.

SYNTAX

INDICATING THAT MODIFY ARRAY EXPECTS TO RECEIVE AN ARRAY OF INTEGERS

IN PARAMETER B AND THE NUMBER OF ARRAY ELEMENTS IN PARAMETER SIZE.

THE SIZE OF THE ARRAY IS NOT REQUIRED BETWEEN

THE ARRAY BRACKETS.

Page 14: Global variables, sorting static variables,function and arrays,
Page 15: Global variables, sorting static variables,function and arrays,

#include <stdio.h>

float average(float a[]);

int main(){

float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};

avg=average(c); /* Only name of array is passed as argument. */

printf("Average age=%.2f",avg);

return 0;

}

float average(float a[ ])

{

int i;

float avg, sum=0.0;

for(i=0;i<6;++i){

sum+=a[i];

}

avg =(sum/6);

return avg;

}

Output Average age=27.08

C program to pass an array containing age of person to a function. This function should find averageage and display the average age in main function.

Page 16: Global variables, sorting static variables,function and arrays,
Page 17: Global variables, sorting static variables,function and arrays,

#include

void Function(int c[2][2]);

int main(){

int c[2][2],i,j;

printf("Enter 4 numbers:\n");

for(i=0;i<2;++i)

for(j=0;j<2;++j){

scanf("%d",&c[i][j]);

}

Function(c); /* passing multi-dimensional array to function */

return 0;

}

Page 18: Global variables, sorting static variables,function and arrays,

void Function(int c[2][2]){

/* Instead to above line, void Function(int c[][2]){ is also valid */

int i,j;

printf("Displaying:\n");

for(i=0;i<2;++i)

for(j=0;j<2;++j)

printf("%d\n",c[i][j]);

} Output

Enter 4 numbers:

2

3

4

5

Displaying:

2

3

4

5

Page 19: Global variables, sorting static variables,function and arrays,