13.2.1 Why do we need Arrays?
దీనికి సమాధానం తెలియాలి అంటే ముందు ఈ కింది ప్రోగ్రాం గురించి డిస్కస్ చేయాలి.
ఇది కొంతమంది విద్యార్థుల మార్కులను రీడ్ చేసి, ఎవరి మార్కులు వారి సరాసరి కన్నా ఎక్కువో అలాంటి వారి సరాసరి ను కాలిక్యులేట్ చేసి ప్రింట్ చేస్తుంది.
#include
int main()
{
int m, i, s = 0, n1 = 0, s1 = 0, n;
printf(“Enter number of students\n”);
scanf(“%d”, &n);
/* reading marks*/
for(i=0; i
printf(“Enter a student marks\n”);
scanf(“%d”, &m);
s = s + m;
}
s = s/n; /* class average */
printf("Enter students marks again\n");
/*again reading students marks to compare with average*/
for(i=0; i
printf(“Enter a student marks\n”);
scanf(“%d”, &m);
if(m > s)
{
s1 = s1 + m;
n1++;
}
}
printf(“Average =%d\n”, s1/n1);
return (0);
}
దీనిలో, మొదటి లూప్ ద్వారా విద్యార్థుల మార్కులను రీడ్ చేసి వారి సరాసరి కనుగొందాం. తర్వాత, రెండో లూప్ ద్వారా మరలా విద్యార్థుల మార్కులను రీడ్ చేసి, ఇంతకు ముందు కనుక్కొన్న సరాసరి తో కంపేర్ చేస్తూ ఎక్కువ ఉన్న వారి మొత్తంమార్కులు, అలాంటి వారు ఎంత మంది ఉన్నారో కనుగొందాం. ఆ తర్వాత, వారి సరాసరి కనుక్కొని ప్రింట్ చేస్తాం.
పైన ఇచ్చిన ప్రోగ్రామును రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.
పై ప్రోగ్రాంలో విద్యార్థుల మార్కులను రెండు సార్లు keyboard నుంచి input ఇవ్వాల్సి ఉంటుంది. అలా కాకుండా ఉండాలంటే, మొదటిసారి రీడ్ చేసినప్పుడు ఒక arrayలో పెడితే వాటిని (విద్యార్థుల మార్కులను) రెండో సారే కాకుండా ఎన్ని సార్లయినా వాడుకోవచ్చు.అప్పుడు మన ప్రోగ్రాం వేగంవతమవుతుంది. అలా, ప్రోగ్రాంలో array లను వాడితే ప్రోగ్రాం ఎఫిషియంట్ అవుతుంది.
ఈ కింద ఇచ్చిన పద్ధతులలో integer array ను declare చేయవచ్చు.
Array size ఎప్పుడూ positive integer constant గా మాత్రమే ఉండాలి. అనగా, 20 బదులు ఇంకేదైనా integerను మాత్రమే వాడాలి. పైన ఇచ్చిన declaration వల్ల 20 integers కు కావలసినంత memory ‘X’కు allocate అవుతుంది.
పైన ఇచ్చిన declaration వల్ల 20 integers కు కావలసినంత memory ‘X’కు allocate అవుతుంది. ఇక్కడ Symbolic constantను వాడి Array size చెప్పాం.
3. ఈ కింద చూపిన విధంగా కూడా, integer arrayను declare చేస్తూ దానికి విలువలును assign చేయవచ్చు.
పైన ఇచ్చిన declaration వల్ల memory ‘X’కు allocate అయి, అందులో పై బొమ్మలో చూపినవిధంగా విలువలు స్టోర్ అవుతాయి; మిగిలిన elements లలో garbage ఉంటుంది.
4. ఇలా కూడా integer arrayను declare చేస్తూ దానికి విలువలును ఈ కింద చూపిన విధంగా assign చేయవచ్చు.
పైన ఇచ్చిన declaration వల్ల కూడా memory ‘X’కు allocate అయి, అందులో పై బొమ్మలో చూపిన విధంగా విలువలు స్టోర్ అవుతాయి; మిగిలిన elements లలో garbage ఉంటుంది.
5. ఈ కింద చూపిన విధంగా integer arrayను దాని size ఇవ్వకుండా declare చేస్తూ విలువలును ఈ కింద చూపిన విధంగా assign చేయవచ్చు.
పైన ఇచ్చిన declaration వల్ల ‘X’కు నాలుగు integersకు ఎంత memory కావాలో అంతే allocate అవుతుంది.
ఈ కింద ఇచ్చినవన్నీ integer arrayను declare చేసేటప్పుడు వాడకూడని పద్ధతులు.
పైన చెప్పినవి float, double, long, మొదలగు టైపు arrays కూడా వర్తిస్తాయి.
Example 1: Array ను ఉపయోగించి ప్రోగ్రాం ఎలా రాయాలో పైన చూశాం. ఇందులో విద్యార్థుల మార్కులను ఒక్కసారే రీడ్ చేస్తున్నాం.
#include
int main()
{
int a[10], i, s = 0, n1 = 0, s1 = 0, n;
printf(“Enter number of students\n”);
scanf(“%d”, &n);
/* reading marks*/
for(i=0; i
scanf(“%d”, &a[i]);
s = s + a[i]; /* Marks are added to s */
}
s = s/n; /* Class average is calculated */
for(i=0; i
if(a[i] > s) { s1 = s1 + a[i];
n1++;
}
}
printf(“Result=%d\n”, s1 / n1);
/* Averge of those students whose marks are more than the class average is calculated */
return (0);
}
పైన ఇచ్చిన ప్రోగ్రాం రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది
Example 2: ఈ ప్రోగ్రాం ఒక set of integers ను integer array లోకి రీడ్ చేసి రిపీట్ కాని నంబర్లను ప్రింట్ చేస్తుంది.
Solution: ముందు ఒక లూప్ ద్వారా ఓ set of integersను integer array లోకి రీడ్ చేస్తాం. తర్వాత, Outer లూప్ ద్వారా array లో ఉన్న ఎలిమెంట్స్ ను ఒక్కోటి తీసుకొని inner లూప్ ద్వారా అన్ని ఎలిమెంట్స్ తో కంపేర్ చేస్తాం.
#include
int main()
{
int i, n, a[10], j, c;
printf(“Enter a number\n”);
scanf(“%d”, &n);
for(i=0; i
scanf(“%d”, &a[i]);
}
printf(”Unique numbers are:\n”);
for(i=0; i
c = 0; /* Counter is initialized to 0 */
for(j=0; j
/* comparing ith element with all other elements */
if(a[i] == a[j]) c++;
}
if( c == 1) printf(“%d\n”, a[i]);
}
return (0);
}
పైన ఇచ్చిన విలువలు arrayలో ఉన్నాయి .అనుకుంటే, కింద టేబుల్ ద్వారా ట్రేస్ చేశాం.
పైన ఇచ్చిన ప్రోగ్రామును రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.
Example 3: ఈ ప్రోగ్రాం ‘n’ students marks, identification నంబర్లను రెండు one dimensional integer arrays లోపలికి రీడ్ చేసి first class వచ్చిన వారి identification నంబర్లను, second class వచ్చిన వారి identification నంబర్లను, third class వచ్చిన వారి identification నంబర్లను ప్రింట్ చేస్తుంది. ఫస్ట్, సెకండ్, థర్డ్ క్లాస్ లకు రావలసిన కనీస మార్కులు 60, 50, 35.
ఉదాహరణకు, మార్కులు, identification నంబర్లు, పైన చూపిన విధంగా ఉన్నాయి అనుకుంటే,
మనకు కింద ఇచ్చిన రిజల్ట్స్ రావాలి.
111 112 142 197
114
117
Solution: ముందు ఓ లూప్ తో డాటా రీడ్ చేశాం. తర్వాత మూడు లూప్ లు వాడాం. మొదటి లూప్ లో ప్రతీ విద్యార్థి మార్క్ ను 60 తో కంపేర్ చేసి, ఎక్కువ అయితే వారి identification నంబర్లును ప్రింట్ చేశాం. అలాగే, మరలా ప్రతీ విద్యార్థి మార్క్ ను సెకండ్ క్లాసా కాదా చెక్ చేసి, అయితే వారి identification నంబర్లును ప్రింట్ చేశాం. ఆ తర్వాత మరలా ప్రతీ విద్యార్థి మార్క్ ను ధర్డ్ క్లాసా అని చెక్ చేసి, అయితే వారి identification నంబర్లును ప్రింట్ చేశాం.
#include
int main()
{
int i, n, a[10], id[10];
printf(“Enter number of students\n”);
scanf(“%d”, &n);
for(i=0; i
scanf(“%d%d”, &a[i], &id[i]);
}
/* Prints ID’s of first class students */
printf(“First class students IDs\n”);
for(i=0; i
}
/* Prints ID’s of second class students */
printf(“\nSecond class students Ids\n”);
for(i=0; i
}
/* Prints ID’s of third class students */
printf(“\nThird class students Ids\n”);
for(i=0; i
}
return (0);
}
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.
Example 4: ఈ ప్రోగ్రాం ‘n’ students marks, identification నంబర్లను రెండు one dimensional integer arrays లోకి రీడ్ చేసి highest marks వచ్చిన వారి identification నంబర్లను, second highest marks వచ్చిన వారి identification నంబర్లను ప్రింట్ చేస్తుంది.
ఉదాహరణకు, మార్కులు, identification నంబర్లు, పైన చూపిన విధంగా ఉన్నాయి అనుకుంటే, మనకు కింద ఇచ్చిన రిజల్ట్స్ రావలెను.
112 118 197
142 132
Solution: ముందు ఒక లూప్ తో డాటా రీడ్ చేశాం. తర్వాత లూప్ లో highest marks, second highest marks కనుగొందాం. తర్వాత లూప్ లో ప్రతీ విద్యార్థి మార్కులను highest marks తో కంపేర్ చేసి సమానం అయితే వారి identification నంబర్లును ప్రింట్ చేశాం. అలాగే, మరలా ప్రతీ విద్యార్థి మార్కులను second highest marks తో సమానమా అని చెక్ చేసి, సమానం అయితే వారి identification నంబర్లును ప్రింట్ చేశాం.
#include
int main()
{
int i, n, a[10], id[10], max1 = 0, max2;
printf(“Enter number of students\n”);
scanf(“%d”, &n);
/* reading marks and identification numbers*/
for(i=0; i
scanf(“%d%d”, &a[i], &id[i]);
}
/* finding highest and second highest marks*/
for(i=0; i
max2 = max1;
max1 = a[i];
}
else if(a[i] > max2 && a[i] < max1){
max2 = a[i];
}
}
/* Each student mark is compared with max1. If matches, his ID is printed */
printf(“Ids of students who got highest marks\n”);
for(i=0; i
printf(“Ids of students who got second highest marks\n”);
/* Each student mark is compared with max2. If matches, his ID is printed */
for(i=0; i
}
పైన ఇచ్చిన ప్రోగ్రామును రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.
13.2.2 Arrays other than integer 1-D arrays
float, long, double టైప్ 1-D arraysను కూడా వాడవచ్చు. అన్ని notations వీటితో వాడుకోవచ్చు. ఈ కింది ఉదాహరణలో float array ను ఎలా వాడాలో చూపించాం.
Example 5: ఈ ప్రోగ్రాం ఒక polynomial coefficient ను ఒక float array లోపలికి రీడ్ చేసిన తర్వాత polynomial విలువ, derivative విలువలను మనం ఇచ్చిన point ‘x’ దగ్గర కాలిక్యులేట్ చేసి ప్రింట్ చేస్తుంది.
#include
#include
int main()
{
int i, n;
float c[10], x, f, ff;
printf(“The order of the polynomial\n”);
scanf(“%d”, &n);
/* nth order polynomial contains n+1 coefficients or terms */
for(i=0; i <= n; i++) {
printf(”Enter coefficient of term %d\n”, i);
scanf(“%f”, &c[i]);
}
printf(“Enter a value for x\n”);
scanf(“%f”, &x);
f = c[0];
ff = 0;
for(i = 1; i <= n; i ++){
f += c [ i ] * pow( x, i );
ff += i * c [ i ] * pow( x, i - 1);
}
printf(“Polynomial Value=%f, Derivative= %f \n”, f, ff);
return (0);
}
ఈ కింద polynomial ను ఉదాహరణగా తీసుకొని పై ప్రోగ్రాం ఎలా పని చేస్తుందో చూపించాం. ఇక్కడ x విలువ 2 అని అనుకున్నాం.
పైన ఇచ్చిన ప్రోగ్రామును రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.
NB Venkateswarlu
M.Tech(IIT-Kanpur)
Ph.D (BITS),PDF(UK),
Sr.Prof., CSE, AITAM, Tekkali