Array

4
  Computer Programming Computer Engineering Department NAME:  ________________________________ COURSE/YR/SEC:  ____________ DATE:  __________  ONE DIMENSIONAL ARRAY OBJECTIVES:  The students should be able to: 1. Learn how to write a program in C Language using array. 2. Learn the one dimensional array. ILLUSTRATION: The illustration above shows how does array works. An integer variable Numbers is an array with an index from 0 up to number of array index declared minus one. Ex. Int Numbers[4]; possible populated array is: //Populating the variable array Numbers[4]    > 4-1 = 3: Therefore Numbers[0]=0; //index 0 first element Numbers[1]=0; //index 1 Numbers[2]=0; //index 2 Numbers[3]=0; //index 3 last element INSTRUCTIONS: Step1. Write the program below. Save and name it as “act_array1 ”. /*Name: <Write your full Name Here.> Date Created: <Write the Date Today Here> Program Name: one dimensional array one*/ #include<stdio.h> #include<conio.h> void main() { int Numbers[5]; int ctr; clrscr(); //Below for loop will allow the user to input values for five times for(ctr=0;ctr<5;ctr++) { printf(“Input value for Numbers[%d]: ”,ctr); scanf(“%d”,&Numbers[ctr]);  } //Below for loop will allow the user to input values for five times for(ctr=0;ctr<5;ctr++) { printf(“The value of Numbers[%d]: %d”,ctr,Numbers[ctr]);  } getch(); } Step 2. Compile the program and then run. Provide the needed input for the scanf()’s and then write the output below.

description

comp

Transcript of Array

  • Computer Programming

    Computer Engineering Department

    NAME:________________________________COURSE/YR/SEC:____________DATE:__________

    ONE DIMENSIONAL ARRAY

    OBJECTIVES: The students should be able to:

    1. Learn how to write a program in C Language using array.

    2. Learn the one dimensional array.

    ILLUSTRATION:

    The illustration above shows how does array works. An integer variable

    Numbers is an array with an index from 0 up to number of array index

    declared minus one. Ex. Int Numbers[4]; possible populated array is:

    //Populating the variable array Numbers[4] > 4-1 = 3: Therefore

    Numbers[0]=0; //index 0 first element

    Numbers[1]=0; //index 1

    Numbers[2]=0; //index 2

    Numbers[3]=0; //index 3 last element

    INSTRUCTIONS:

    Step1. Write the program below. Save and name it as act_array1.

    /*Name:

    Date Created:

    Program Name: one dimensional array one*/

    #include

    #include

    void main()

    {

    int Numbers[5];

    int ctr;

    clrscr();

    //Below for loop will allow the user to input values for five times

    for(ctr=0;ctr

  • Computer Programming

    Computer Engineering Department

    Step 3. Edit the program and change int Numbers[5]; in the variable declaration into int Numbers[10];. The index of the declared array was change from 5 and turning into 10. Save, compile and run the

    program. Are there 10 inputs and 10 outputs on your display? If yes

    proceed to next step or else change the for(ctr=0;ctr

  • Computer Programming

    Computer Engineering Department

    Step 8. Edit and change the printf("%c",pwd[ctr]); above the delay(300); into printf("\npwd[%d]=%c",ctr,pwd[ctr]);. Save, compile and run the program. What did you observe on your output?

    _____________________________________________________________________

    _____________________________________________________________________

    Step 9. Run again the program and write the output below.

    Step 10. Edit and change pwd[ctr]=getch(); into pwd[ctr]=getche();. Save, compile and run the program. Input any values as the password and then write the output below.

    Step 11. Edit the program and disable the code printf("*"); by making it into comments. Save, compile and run the program. Input any values

    as the password and then write the output below.

    Step 12. What is the use of getch() and getche() in the array variable

    pwd[10]?. What did you observe about the difference of the two? Write your answer below respectively.

    _____________________________________________________________________

    _____________________________________________________________________

    _____________________________________________________________________

    _____________________________________________________________________

    _____________________________________________________________________

    _____________________________________________________________________

    _____________________________________________________________________

    Step 13. What preprocessor directives does getche() belongs?

    _____________________________________________________________________

    Step 14. Write the program below. Save and name it as act_array2.

    /*Name:

    Date Created:

    Program Name: one dimensional array two*/

    #include

    #include

    void main()

    {

    //Initializing values for one dimensional array

    int age[5]={12,13,10,24,15};

    char name[5]={'j','e','s','u','s'};

    float decmal[5]={1.2,1.33,2.45,3.89,99.9};

    int ctr;

    clrscr();

  • Computer Programming

    Computer Engineering Department

    //This will output the values of array variable age and its index.

    for(ctr=0;ctr