Showing posts with label character arrays in c. Show all posts
Showing posts with label character arrays in c. Show all posts

Saturday, October 30, 2010

Arrays in C++ programming:program that will store numbers or values in array? turbo c program how to accept 10 integer



Array Basics in C++ programming language:
Arrays are extremely important for every computer programming language including C++. There are situations when we need arrays in every programming language. In this chapter I will explain the basics of arrays in C++ programming language.
When we need an array?
When many variables are required of same data type and they all need to be treated the same way. For example: To store the first 10 even numbers we can declare an array of size 10.
Why we need array?
In the above example: rather than declaring 10 different integers, we can declare an array with no headache of memorizing 10 different variable names.
What is an array?
Array is a collection of variables of same data type under a single name.
How we use array?
There are different kinds of arrays: 1D, 2 D, 3D [D=Dimensions] for each data type. But here I am going to explain 1D array for integer or float data type. Char data type arrays have a different approach. I will explain them in the different chapters.
For example: if we need to store the first 10 even numbers, we can declare an array of integers like this:

rather than creating 10 variables like int a,b,c,d,e,f,g,h,I,j;
In memory 10 integers are declared in sequential memory locations:
In an array subscript or index always starts with ZERO. The subscripts are used by the compiler to locate the array item. For example to refer to the values 2,4,6,…,20 the compiler refers them in this way:
arr[0], arr[1], arr[2],… arr[9]
Try this code in C++ compiler.
//Program to store first 10 even numbers and print them
#include<iostream.h>
void main()
{
int arr[10], i,val;
/* In the following loop i is used to run the loop 10 times. i is initialized with ZERO because subscript starts with ZERO. val is used to store values starting from 2 and incrementing by 2 each time.*/
for(i=0,val=2; i<=9;i++,val=val+2)
arr[i]=val;
for (i=0;i<=9;i++)
cout<<arr[i]<<endl;
}
The Output of this program is:
2
4
6
8
10
12
14
16
18
20
The above C++ program is run in turbo c compiler.
The integer and float arrays have the same concept. The only difference is of their data types. integers store whole numbers while floats store decimal numbers.
Character arrays have a different concept which I have explained in this article:



character or char arrays as data structures in c: string reverse program and palindrome program using c programming

c programming arrays ---> data structures in c: char arrays in c programming
C programming arrays:
Arrays are important data structures in C.
Arrays are very much useful in situations when we need to store many variables of same data type and treat them similarly. For understanding c programming arrays and arrays as data structures in c, read the hub “Array Basics in C programming”
I am explaining here only character arrays in c programming.
What is an array?
An array is collection of similar type of data items.
And a character array is called a string.
Character arrays in C programming have a different concept from integer arrays or float arrays in C. Character arrays are declared in the same way as integer or float arrays but one thing that is different in character arrays is that a NULL character (‘\0’) is appended automatically at the end of the character array.
char arr[10];
the above statement declares a character array named arr with 10 continuous memory locations each occupying 1 byte because the base size of a character data type is 1 Byte. So the above array takes 10 Bytes in memory.
Now a string (group of character or a character array is called a string) is input in array by the following statement.
cin>>arr;
In the above input statement the string does not contain any whitespace because cin>> ends the string where a whitespace is encountered. If whitespaces are to be input then the following statement can be used
gets(arr);
do not forget to include stdio.h file to let the gets() function work
Suppose if the user inputs “HELLO” from the keyboard. The actual storage in memory would be :

c programming arrays: arrays as data structures in c : sample programs

A NULL Character(‘\0’) or string terminator is automatically appended after the last character of the string. It (‘\0’) marks the end of the string. This does not happen in integer or float arrays in C++.
Example 1:

c programming arrays: char arrays as data structures in c: sample program to reverse a string and count number of characters in string

/* program to input a string then count number of characters in string and

print the reverse of the string*/

#include<stdio.h> //for printf , scanf and other functions

#include<conio.h> //for clrscr() and getch()functions

void main()

{

clrscr();

char arr[10];

printf("Enter A String\n");

gets(arr);

for(int i=0;arr[i]!='\0';i++);//empty loop which does not have a body

printf("Length of the string is %d", i);

//now reverse print of the string

for(i--;i>=0;i--)

printf("%c",arr[i]);

getch();

}

c programming arrays: char arrays as data structures in c: sample program 1 output

OUTPUT:
Enter A String
MADAM
String is palindrome
Example 2:

c programming arrays: char arrays as data structures in c: sample program to check a string is palindrome or not

/*Program to input a string then check whether string is palindrome or not
A palindrome string is same as its reverse like RADAR, NITIN, MADAM */

#include<stdio.h>  //for printf, scanf,gets  and other functions
#include<conio.h>  //for clrscr() and getch()functions
void main()
{
clrscr();

char arr[10];
int i,j,flag=1;
printf("\n Enter A String\n");

gets(arr);

for(i=0;arr[i]!='\0';i++);//empty loop which does not have a body
for(j=0,i--;i>=0;i--,j++)//i moves backward and j moves forward
{
 if(arr[i]!=arr[j])
 {
  flag=0;
  break;
 }
}
if(flag==1)
printf("String is palindrome");
else
printf("String is not palindrome");

getch();
}

c programming arrays: char arrays as data structures in c: sample program 2 output

OUTPUT:
Enter A String
MADAM
String is palindrome