Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

Thursday, 16 June 2011

An Array of String (Security)

#define MAX 5
#define LEN 40


void main(void)
{
 int dex;
 int enter=0;
 char name[40];
 static char list[MAX][LEN]={ "Muzammil", "Nigel", "Katrina",
                              "Alistair" };
 printf("Enter your name:"):
 gets(name);
 for(dex=0;dex<MAX;dex++)
  if(strcmp(&list[dex][0],name)==0)
   enter = 1;
 if(enter==1)
   printf("You may enter, oh honored one.");
 else
   printf("Guards! Remove this person!");
 }


Output:
There are two possible outcomes when you interact with this program. Either your name is on the list:


Enter your name: Muzammil
You may enter, oh honored one.


or it isn't:


Enter your name: George
Guards! Remove this person!

Wednesday, 1 June 2011

Sorting an Array (increasing order)

#define MAXSIZE 20
void sort(int[],int);


void main (void)
{
 static int list[MAXSIZE];
 int size=0;
 int dex;
 do
  {
   printf("Type Number: ");
   scanf("%d",&list[size]);
  } 
 while(list[size++] != 0);
 sort(list,--size);
 for(dex=0;dex<size;dex++)
  printf("%d\n",list[dex]);


}
void sort(int[list],int size)
 {
  int out,in,temp;
  for(out=0;out<size-1;out++)
   for(in=out+1;in<size;in++)
    if(list[out]>list[in])
      {
       temp = list[in];
       list[in] = list[out];
       list[out] = temp;
      }
}


Output:
Type Number: 46
Type Number: 25
Type Number: 73
Type Number: 58
Type Number: 33
Type Number: 18
Type Number: 0
18
25
33
46
58
73


If you have any question, you ask me freely!

Tuesday, 31 May 2011

Which Number is Largest? / Array

#define MAXSIZE 20
int max(int[], int);
void main()
{
 int list[MAXSIZE];
 int size=0;
 int num;
 do
  {
   printf("Type number: ");
   scanf("%d",list[size]);
  }
 while(list[size++] != 0);
 num=max(list,size-1);
 printf("Largest Number is %d",num);
}
int max(int list[],int size)
{
  int dex,max;
  max = list[0];
  for(dex=1;dex<size;dex++)
   if(max < list[dex])
    max = list[dex];
  return(max);
}


Output:


Type Number: 42
Type Number: 1
Type Number: 64
Type Number: 33
Type Number: 27
Type Number: 0 
Largest Number is 64



Monday, 30 May 2011

Arrays

If you have collection of similar data elements, you may find it inconvenient to give each one a unique variable name. For instance, suppose you wanted to find the average temperature for a particular week. If each day's temperature had a unique variable name, you would end up reading in each value separately like this:


printf("Enter Sunday Temperature: ");   
scanf("%d",&suntmp);
printf("Enter Monday Temperature: ");
scanf("%d",&montmp);
---------------


and so on for each day of the week, with an expression for the average such as this:


(suntmp+montmp+tuetmp+wedtmp+thutmp+fritmp+sattmp)/7


This is on altogether unwieldy business, especially if you want to average the temperature for a month or a year.
 Clearly we need a convenient way to refer to such collections of similar data elements. The array fills the bill. It provides a way to refer to individual items in a collection by using the same variable name, but differing subscripts, or numbers. Let's see how we'd solve our problem of averaging the temperature for a week using arrays:


void main()
{
 int temper[7];
 int day,sum;


 for(day=0;day<7;day++)
 {
  printf("Enter Temperature for day %d: ",day);
  scanf("%d",&temper[day]);
 }

 sum=0;
 for(day=0;day<7;day++)
  sum+=temper[day];
 printf("Average is %d.",sum/7);





This program reads in seven temperatures, stores them in an array and then, to calculate an average temperature, reads them back out of the array, adding them together, and divide by 7. Here's a result:


Enter Temperature for day 0: 74
Enter Temperature for day 1: 76
Enter Temperature for day 2: 77
Enter Temperature for day 3: 77
Enter Temperature for day 4: 64
Enter Temperature for day 5: 66
Enter Temperature for day 6: 69
Average is 71.


If you have any question, you can ask me freely!