Anda di halaman 1dari 14

INTRODUCTION

As discussed in the previous post, there are two types of data structures
available to C/C++ programmers. One is already built into the programming
language and other one is a bit complex in a sense that it can be implemented
using the built in data structures and data types. In C/C++ programming
language, built in data structures include Arrays, structures, unions and classes.
Some of the examples of complex data structures are Stack, Queue, Linked List,
Tree and Graph.

The aim of this first tutorials is to teach you how to declare, initialise and use
simple arrays as well as multidimensional arrays. You will also be able to use
arrays as data structure in your C/C++ program.

Array is a very basic data structure provided by every programming language.


Let’s talk about an example scenario where we need to store ten employees’
data in our C/C++ program including name, age and salary. One of the solutions
is to declare ten different variables to store employee name and ten more to
store age and so on. Also you will need some sort of mechanism to get
information about an employee, search employee records and sort them. To
solve these types of problem C/C++ provide a mechanism called Arrays.

An array is simply a number of memory locations, each of which can store an


item of data of the same data type and which are all referenced through the
same variable name. Ivor Horton.

Array may be defined abstractly as finite order set of homogeneous elements. So


we can say that there are finite numbers of elements in an array and all the
elements are of same data type. Also array elements are ordered i.e. we can
access a specific array element by an index.
PROBLEMS

1. Write a C++ program that prints a 16 x 16 table including all characters and
their respective codes on the screen.

2. Create a C++ program that receives one number as N and prints all divisor of
the number ont the screen (Divisors of N is an integer that evently divides N
without leaving a remainder).

3. Write a C++ program that receives X as a number and then N as number of


literation. Then it calculates and prints f according to the following formula:

4. Write a C++ program that receives 10 float numbers from user and store them
in an array. then it prints the array on the screen from last cell to the first one.

5. Write a code that receives 10 integer numbers from user and stores them in an
array. Then it receives one number and tells the user whether this number
included in the array or not.

6. Write a C++ program receives a name as a string from user and prints the
number of its letters (Known as string’s length).

7. Write a C++ program that receives a string from user inverse it and prints it on
the screen.

8. Write a C++ program that receives two 50-digits numbers. Then it calculates
and prints sum of them in the screen. Note that you must define a 50-cell array
for each number and use each cell for one digit.

9. Write a C++ program that receives one complete sentence from user. invert
each word in the sentence inside itself. Then prints the result on the screen.

10. Write a C+ program that generates 100 random numbers and put them in an
array. then it sorts the array ( ascending) and removes the repetitive numbers.
Then the progeram prinst the result on the screen.

reate a c++ program that will desplay " i will pass PRGGLF" center of the screen.
SOLUTIONS TO THE PROBLEMS

1. Write a C++ program that prints a 16 x 16 table including all characters


and their respective codes on the screen.
2. Create a C++ program that receives one number as N and prints all
divisor of the number ont the screen (Divisors of N is an integer that
evently divides N without leaving a remainder).
3. Write a C++ program that receives X as a number and then N as number of
literation. Then it calculates and prints f according to the following formula:
4. Write a C++ program that receives 10 float numbers from user and store them
in an array. then it prints the array on the screen from last cell to the first one.
5. Write a code that receives 10 integer numbers from user and stores them in an
array. Then it receives one number and tells the user whether this number
included in the array or not.
6. Write a C++ program receives a name as a string from user and prints the
number of its letters (Known as string’s length).
7. Write a C++ program that receives a string from user inverse it and prints it on
the screen.
8. Write a C++ program that receives two 50-digits numbers. Then it calculates
and prints sum of them in the screen. Note that you must define a 50-cell array
for each number and use each cell for one digit.
#include<iostream> if(B[y]=='0')
using namespace std; D[y]=0;
char A[51],B[51]; else if(B[y]=='1')
int C[51],D[51]; D[y]=1;
int x,y,z,first,second; else if(B[y]=='2')
main() D[y]=2;
{ else if(B[y]=='3')
cout<<"Enter first 50 digit D[y]=3;
number: "; else if(B[y]=='4')
cin.getline(A,51); D[y]=4;
cout<<"Enter second 50 else if(B[y]=='5')
digit number: "; D[y]=5;
cin.getline(B,51); else if(B[y]=='6')
for(x=0;x<51;x++) D[y]=6;
{ if(A[x]=='0') else if(B[y]=='7')
C[x]=0; D[y]=7;
else if(A[x]=='1') else if(B[y]=='8')
C[x]=1; D[y]=8;
else if(A[x]=='2') else if(B[y]=='9')
C[x]=2; D[y]=9;
else if(A[x]=='3') }
C[x]=3; x=0;
else if(A[x]=='4') y=0;
C[x]=4; for(z=0;z<51;z++)
else if(A[x]=='5') { first=first+C[x];
C[x]=5; second=second+D[y];
else if(A[x]=='6') x++;
C[x]=6; y++; }
else if(A[x]=='7') cout<<"Sum of the first 50 digit numbers:
C[x]=7; "<<first<<'\t';
else if(A[x]=='8') cout<<"Sum of the second 50 digit numbers:
C[x]=8; "<<second;
else if(A[x]=='9') cout<<'\n'<<"The sum of first and second 50 digit
C[x]=9; number: "<<first+second;
} cout<<'\n';
for(y=0;y<51;y++) system("pause");
}
9. Write a C++ program that receives one complete sentence from user. invert
each word in the sentence inside itself. Then prints the result on the screen.
10. Write a C+ program that generates 100 random numbers and put them in an
array. then it sorts the array ( ascending) and removes the repetitive numbers.
Then the progeram prinst the result on the screen.
DISCUSSION

The array holds a set of homogeneous elements indexed by ordinal value. The
actual contents of an array are laid out as a contiguous block, thereby making
reading from or writing to a specific array element very fast. In addition to the
standard array.

The ArrayList provides a more flexible array-like data structure. Rather than
enforcing homogeneous types, the ArrayList allows for heterogeneous types to
be stored by using an array of objects. Furthermore, the ArrayList does not
require explicit allocation and can gracefully grow as the more elements are
added.

The common problems that I had encounter was the syntax error and resolve it
though double checking my codes and arranging it on indent form so that I could
easily trace or locate where is my error.

Another one is the formula coding. Sometimes it gives a wrong output. I trouble
shot it by putting some open and close parenthesis. Never the less there are no
major problems in this complex problems.
REFFERENCE

Program Logic Formulation in C

http://www.cplusplus.com/info/history/

http://purecode.wordpress.com/2007/12/03/data-structures-array-
arraylist-generics-list/

http://msdn.microsoft.com/en-
us/library/aa289148%28VS.71%29.aspx

Anda mungkin juga menyukai