Sunteți pe pagina 1din 9

UET Taxila Computer system & Programming

Mechanical Engineering Dept

Objective
Make use of

ARRAYS:
An array allows you to store and work with multiple values of the same data type.

The variables you have worked with so far are designed to hold only one value at a
time. A variable causes only enough memory to be reserved to each of the variable to
hold a single value.
An array works like a variable that can store a group of values, all of the same data
type. The values are stored together in consecutive memory locations. Here is a
definition of an array of integers:

int days[6];

The name of this array is days. The number inside the brackets is arrays size
declaratory.it indicates the number of elements, or values, the array can hold. The days
array can store six elements, each one is an integer. An arrays size should be an
integer value.
The following examples are of declaring a valid array.
const int NUM_DAYS = 6;
int days[NUM_DAYS];

The following are all valid array of any data type


float temperatures[100]; // Array of 100 floats
string names[10]; // Array of 10 string objects
long units[50]; // Array of 50 long integers
double sizes[1200]; // Array of 1200 double

56
UET Taxila Computer system & Programming
Mechanical Engineering Dept

Program No.36
Write a program in C that take age of five persons and then just display the age of
each person by using arrays.

Code

57
UET Taxila Computer system & Programming
Mechanical Engineering Dept

Program No.37
Write a Program that takes array elements from user and then transfer those
elements to another array. Size of array will be 10

Code:

58
UET Taxila Computer system & Programming
Mechanical Engineering Dept

Program No.38
Write a program that takes 5 array elements from user. Swap position [2] element
with position [4] element.

Code

59
UET Taxila Computer system & Programming
Mechanical Engineering Dept

Program No.38
Write a program to input data into two different arrays and then to add the two arrays
and store the result in the third array.

Code

60
UET Taxila Computer system & Programming
Mechanical Engineering Dept

Program No.39
write a program in C, to input data into an array. Enter a value from the Keyboard
and find out the location of the entered value in the array. If the entered number is found
in the array, display the message "Number Foundelse display Number Not Found

Code

61
UET Taxila Computer system & Programming
Mechanical Engineering Dept

Program No.40
Find the Average of 10 numbers entered in an array. The Array should be controlled by
while loop.

Code

62
UET Taxila Computer system & Programming
Mechanical Engineering Dept

7.10 Strings
A string constant is a one-dimensional array of characters terminated by a null ( \0 ).
For example,

char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ;

7.10.1 Example of Strings

/* Program to demonstrate printing of a string */


main( )
{
char name[ ] = "Rameez" ;
int i = 0 ;
while ( i <= 7 )
{
printf (%c,name[i]);
i++ ;
}
}

63
UET Taxila Computer system & Programming
Mechanical Engineering Dept

Program No.41
Write a program that gets the name from user and then print back on the screen.

The String I/O Function gets() & puts()


scanf & printf are not so versatile because they ignore charters after white space, so
to over come this problem C/C++ uses puts & gets function from library file
<string.h>

Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char name[15];
puts("Enter the name of user");
gets(name);
puts("The name you enter is");
puts(name);
getch();
}

64

S-ar putea să vă placă și