Array Examples
Simple Example
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define TALMID_SIZE 4
void main()
{
int i;
int talmid[TALMID_SIZE];
float average,sd ;
clrscr();
read grades and compute average
printf ("Enter %d grades\n\n",TALMID_SIZE - 1);
average=0.0;
for (i=1 ; i<TALMID_SIZE; i++)
{
printf("%d==> ",i);
scanf("%d",&talmid[i]);
average += talmid[i];
}
average/=float(TALMID_SIZE - 1);
printf("\n%f",average );
/*-----------------------------------------*/
/* compute standard deviation */
/*-----------------------------------------*/
sd=0.0;
for (i=1 ; i<TALMID_SIZE; i++)
sd +=(talmid[i]-average)*(talmid[i]-average);
sd=sqrt(sd/float(TALMID_SIZE - 1 ));
printf ("\nthe standard deviation is==>%2.3f",sd);
}
Standard Dev.
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define TALMID_SIZE 4
float sd( int [],int);
void main()
{
int i;
int talmid[TALMID_SIZE];
clrscr();
read grades
printf ("Enter %d grades\n\n",TALMID_SIZE - 1);
for (i=1 ; i<TALMID_SIZE; i++)
{
printf("%d==> ",i);
scanf("%d",&talmid[i]);
}
printf("\nthe standard deviation is==>%2.3f",sd(talmid,TALMID_SIZE));
}
standard deviation function
float sd(int v[],int v_size)
{
float a,s;
int i;
a=0.0;
for (i=1 ; i<v_size; i++)
a += v[i];
a/=float(v_size - 1);
compute standard deviation
s=0.0;
for (i=1 ; i<v_size; i++)
s +=(v[i]-a)*(v[i]-a);
s=sqrt(s/float(v_size - 1 ));
return s;
}
Days in month
#include <conio.h>
#include <stdio.h>
int days_in_month(int,int);
void main()
{
int m,y;
clrscr();
printf("enter month year==>");
scanf("%d %d",&m,&y);
printf(" %d",days_in_month(m,y)) ;
}
compute no of days in a month
int days_in_month(int month , int year)
{
int v[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if( month!=2 )
return v[month];
else if( (year % 4) == 0 )
return 29;
else return 28;
}
Vertical histogram
#include <conio.h>
#include <stdio.h>
#define TALMID_SIZE 20
void main()
{
int i,j;
int talmid[TALMID_SIZE],talmid_no_of_stars[TALMID_SIZE];
clrscr();
read grades and compute no of stars
printf ("Enter %d grades\n\n",TALMID_SIZE - 1);
for (i=1 ; i<TALMID_SIZE; i++)
{
printf("%d==> ",i);
scanf("%d",&talmid[i]);
if (talmid[i] <=54)
talmid_no_of_stars[i]=1 ;
else
talmid_no_of_stars[i]=talmid[i]/5 - 9;
}
print histogram
for(j=11;j>0 ; j--)
{
printf("\n");
for (i=1 ; i<TALMID_SIZE; i++)
{
if (talmid_no_of_stars[i] >=j )
printf("* ");
else printf(" ");
}
}
}
Recent Stories
Top DiscoverSDK Experts
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
X
Compare Products
Select up to three two products to compare by clicking on the compare icon () of each product.
{{compareToolModel.Error}}Now comparing:
{{product.ProductName | createSubstring:25}} X
{{CommentsModel.TotalCount}} Comments
Your Comment