0% found this document useful (0 votes)
20 views6 pages

Josh

The document contains code for two C++ programs. The first program asks the user to enter 10 numbers and stores them in an array. It then displays a menu with options to manipulate the array such as sorting, finding max/min, and displaying odd/even numbers. The second program uses a structure to store user entries of name, age, gender. It asks the user how many entries and then collects the data, storing it in an array of structures. The entries are then displayed.

Uploaded by

Joshua Felisilda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Josh

The document contains code for two C++ programs. The first program asks the user to enter 10 numbers and stores them in an array. It then displays a menu with options to manipulate the array such as sorting, finding max/min, and displaying odd/even numbers. The second program uses a structure to store user entries of name, age, gender. It asks the user how many entries and then collects the data, storing it in an array of structures. The entries are then displayed.

Uploaded by

Joshua Felisilda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

UNIVERSITY OF SCIENCE AND TECHNOLOGY

OF SOUTHERN PHILIPPINES
Alubijid | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon

IT112 - Computer Programming 1


Activity 10

NAME: Joshua C. Felisilda SECTION: BSIT - 1R12

Do the following.

1. Create a program where it asks for 10 numbers from the user and store it in an
array. The program will then ask or give options for the user as to what to do
next. The options are as follows:
a. Display elements from minimum to maximum
b. Display elements from maximum to minimum
c. Display the maximum number among the elements
d. Display the minimum number among the elements
e. Display both maximum and minimum numbers
f. Display odd and even numbers
g. Exit the program

The program will not end until the user chooses option G, otherwise the program
will always display the options every after execution of an option.

2. Create a program where it will use a structure. The program will ask user for the
number of entries to be stored. The data to be store would be name, age, gender.
After all entries are entered, it will then display all the stored entries.

1.

#include <iostream>
#include <algorithm>
#include <stdlib.h>
using namespace std;

const int Size = 10;

void displayMintoMax(int arr[]){


sort(arr, arr + Size);
cout<<"Elements from minimum to maximum: ";
for (int i = 0; i < Size; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
void displayMaxtoMin(int arr[]){
sort(arr, arr + Size, greater<int>());
cout<<"Elements from maximum to minimum: ";
for(int i = 0; i < Size; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
void maxNumber(int arr[]){
int max = arr[0];
for(int i = 0; i < Size; i++){
if(arr[i] > max){
max = arr[i];
}
}
cout<<"The maximum number among the elements: "<<max<<endl;
}
void minNumber(int arr[]){
int max = arr[0];
for(int i = 0; i < Size; i++){
if(arr[i] < max){
max = arr[i];
}
}
cout<<"The minimum number among the elements: "<<max<<endl;
}
void maxMinNumbers(int arr[]){
int max = arr[0];
int min = arr[0];

for(int i = 0; i < Size; i++){


if(max < arr[i]){
max = arr[i];
}
if(min > arr[i]){
min = arr[i];
}
}
cout<<"This is the maximum value: "<<max<<endl;
cout<<"This is the minimum value: "<<min<<endl;
}
void oddEvenNumbers(int arr[]){
cout<<"Odd numbers: ";
for(int i = 0; i < Size; i++){
if (arr[i] % 2 != 0){
cout<<arr[i]<<" ";
}
}
cout<<endl;
cout<<"Even numbers: ";
for(int i = 0; i < Size; i++){
if(arr[i] % 2 == 0){
cout<<arr[i]<<" ";
}
}
cout<<endl;
}

int main(){
int arr[Size];
cout<<"Enter 10 numbers: "<<endl;
for (int i = 0; i < Size; i++){
cin>>arr[i];
}
system("cls");
system("color a");
cout<<"--------------------------------------------------------\n";
cout<<"Entered numbers: ";
for (int i = 0; i < Size; i++){
cout<<arr[i]<<" ";
}
cout<<"\n-------------------------------------------------------\n\n";

cout<<"========================================================\n";
cout<<"CHOOSE WHAT TO DO WITH THOSE NUMBERS"<<endl;
char choice;

while(true){
cout<<"a. Display elements from minimum to maximum"<<endl;
cout<<"b. Display elements from maximum to minimum"<<endl;
cout<<"c. Display the maximum number among the elements"<<endl;
cout<<"d. Display the minimum number among the elements"<<endl;
cout<<"e. Display both maximum and minimum number among the elements"<<endl;
cout<<"f. Displayodd and even numbers"<<endl;
cout<<"g. exit the program"<<endl<<endl;
cout<<"Enter your choice (a,b,c,...,g): ";
cin>>choice;
system("cls");
switch(choice){
case 'a':
system("cls");
cout<<"*****************"<<endl;
cout<<"initializing..."<<endl;
cout<<"output:"<<endl;
system("color a");
displayMintoMax(arr);
cout<<"--------------------------------------------------------\n\n";
break;
case 'b':
system("cls");
cout<<"*****************"<<endl;
cout<<"initializing..."<<endl;
cout<<"output:"<<endl;
system("color b");
displayMaxtoMin(arr);
cout<<"--------------------------------------------------------\n\n";
break;
case 'c':
system("cls");
cout<<"*****************"<<endl;
cout<<"initializing..."<<endl;
cout<<"output:"<<endl;
system("color c");
maxNumber(arr);
cout<<"--------------------------------------------------------\n\n";
break;
case 'd':
system("cls");
cout<<"*****************"<<endl;
cout<<"initializing..."<<endl;
cout<<"output:"<<endl;
system("color d");
minNumber(arr);
cout<<"--------------------------------------------------------\n\n";
break;
case 'e':
system("cls");
cout<<"*****************"<<endl;
cout<<"initializing..."<<endl;
cout<<"output:"<<endl;
system("color e");
maxMinNumbers(arr);
cout<<"--------------------------------------------------------\n\n";
break;
case 'f':
system("cls");
cout<<"*****************"<<endl;
cout<<"initializing..."<<endl;
cout<<"output:"<<endl;
system("color f");
oddEvenNumbers(arr);
cout<<"--------------------------------------------------------\n\n";
break;
case 'g':
exit(false);
default:
cout<<"******************"<<endl;
cout<<"Invalid Choice"<<endl;
break;
}

}
system("pause");
return 0;
}
2.

#include <iostream>
#include <windows.h>
using namespace std;

struct myData{
private:
char gender;
public:
string name;
int age;

void setInformation(string aN, int aA, char aG){


name = aN;
age = aA;
setGender(aG);

displayInformation();
}
char getGender(){
return gender;
}
void setGender(char aG){
if(aG == 'M' || aG == 'F' || aG == 'O'){
gender = aG;
}else{
gender = '!';
}
}

void displayInformation(){
cout<<"=================="<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
cout<<"=================="<<endl;
}

};

int main(){
int entries;
cout<<"How many Entries do you want to store? _";
cin>>entries;
myData Datas[entries];

//Datas[0].setInformation("EL-El", 15, '8');


for(int i = 0; i < entries; i++){
string name;
int age;
char gender;

cout<<endl<<"Enter a name: ";


getline(cin>>ws, name);

cout<<"Input an Age: ";;


cin>>age;

cout<<"Gender(M = male, F = female, O = Others, ! = Undecided): "<<endl;


cout<<"\t";
cin>>gender;
cout<<endl;

Datas[i].setInformation(name, age, gender);


}

system("cls");
system("color a");
for(int i = 0; i < entries; i++){
cout<<endl;
Datas[i].displayInformation();
}

return 0;
}

NOTE:

ANSWERS SHOULD BE ENCODED IN A DOCUMENT FORMAT. FILES SHOULD


BE LABELED PROPERLY AS TO YOUR NAME AND SECTION. THANK YOU AND
GOODLUCK!!!

You might also like