One Dimensional Array OBJECTIVES: The Students Should Be Able To
One Dimensional Array OBJECTIVES: The Students Should Be Able To
NAME:________________________________COURSE/YR/SEC:____________DATE:__________
#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]);
}
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:
//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.
Computer Programming
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<5;ctr++) into
for(ctr=0;ctr<10;ctr++).
int ctr;
clrscr();
printf("Input your password: ");
//This will allow the user to input 10 password characters.
for(ctr=0;ctr<=10;ctr++)
{
pwd[ctr]=getch();
printf("*"); // ---> this will replace the character into asterisk
Step 4. Save, compile, then run the program. Write the output below.
}
printf("\nPassword: ");
//This will display the password that you inputted.
Step 5. How many variables do we have in the program?
_____________________________________________________________________
Step 6. Write the program below. Save and name it as act_pwd.
/*Name: <Write your full Name Here.>
Date Created: <Write the Date Today Here>
Program Name: Password using array*/
#include<stdio.h>
#include<conio.h>
#include<dos.h>
for(ctr=0;ctr<=10;ctr++)
{
printf("%c",pwd[ctr]);
delay(300);
}
getch();
}
Step 7. Compile and run the program. Input any values as the
password and then write the output below.
void main()
{
char pwd[10];
Computer Engineering Department
Computer Programming
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 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 14. Write the program below. Save and name it as act_array2.
/*Name: <Write your full Name Here.>
Date Created: <Write the Date Today Here>
Program Name: one dimensional array two*/
#include<stdio.h>
#include<conio.h>
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
//This will output the values of array variable age and its index.
for(ctr=0;ctr<5;ctr++)
printf(\nValues of age[%d] is %d, ctr,age[ctr]);
//This will output the values of array variable name and its index.
for(ctr=0;ctr<5;ctr++)
printf(\nValues of name[%d] is %c, ctr,age[ctr]);
//This will output the values of array variable decmal and its index.
for(ctr=0;ctr<5;ctr++)
printf(\nValues of decmal[%d] is %.2f, ctr,age[ctr]);
getch();
Step 16. Write a program using one array variable that will ask 5 users
to input their age and the program will display those age per index of
the said array variable. Write your codes on a separate bond paper
and follow the format below.
/*Name: <Write your full Name Here.>
Date Created: <Write the Date Today Here>
Program Name: My first Array program*/
#include<stdio.h>
#include<conio.h>
void main()
{
}
..your codes here.
Step 15. Compile and run the program. Write the output below.
}
Step 17. Create the needed program above in your c language
compiler. Save and name it as my_array.
Step 18. Compile and run the program. Write the output below.
Step 19. Save all the .cpp and .bak files on a folder. Name the folder as
<Last Name_array1>. Save it on DVD-RW together with the previous
given activity and give it to your professor. Review your answers. Pass
this paper as well once you pass the disc.
Computer Engineering Department