Prog Chapter 8 Answers Part 1

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Program Activiy 1:

#include <iostream>
#include <conio> error/s: 1. #include <conio> : must be
#include <conio.h>
2. using namespace std;
int main () 3. "size" is not declared: must
be int size = 5;
{
int num[5] = {1,2,3,4,5}; Output: kapag na correct na yung
mga mali
double grade[] = {78,85,86,82,85}; 1 2 3 4 5
float ave[5] = {82.4,85.5}; 78 85 86 82 85
int age[5]; 82.4 85.5 0 0 0
age [0] = 16; 16 19 20 18 13
age [1] = 19;
age [2] = 20;
age [3] = 18;
age [4] = 13;
age [5] = 17;

int i;

for (i = 0; i<size; i++)


cout << num[i] << " ";
cout << endl;

for (i = 0; i<size; i++)


cout << grade[i] << " ";
cout << endl;

for (i = 0; i<size; i++)


cout << ave[i] << " ";
cout << endl;

for (i = 0; i<size; i++)


cout << age[i] << " ";
cout << endl;

getch();
return 0;
}

Program Activity 2:

#include <iostream>
#include <stdlib> error/s: 1. #include <stdlib> :
must be #include <stdlib.h>
2. using namespace std;
const int size = 10; 3. on line 10:
cout<<"Enter " <<size"<< numbers: "<<endl; : must be cout<<"Enter numbers: "<< size
<<endl;
4. (optional) on line 22:
cout<<"largest number: "<<max; : must be cout<<"largest number: "<<max << endl;
int main ()
{
int num[size]; Output: kapag na correct na
(input ko ay 9 to 1)
cout<<"Enter " <<size"<< numbers: "<<endl; Enter numbers: 10
for(int i=0;i<size;i++) enter a number: 9
{ enter a number: 8
cout<<"enter a number: "; enter a number: 7
cin>>num[i]; enter a number: 6
} enter a number: 5
int max=num[0]; enter a number: 4
enter a number: 3
for (int j =0;j<size;j++) enter a number: 2
if(max<num[j]) enter a number: 1
max=num[j]; enter a number: 0
largest number: 9
cout<<"largest number: "<<max; press any key to
continue.... (dahil sa system ("pause"))
system("pause");
return 0;
}

Program Activity 3:

#include <iostream>
void ballon(int[]); error/s: 1. using namespace
std;
const int size = 5;

int main () Output:


{ 5 10 15 20 25
int num1[size] = {25,15,20,10,5};
ballon(num1);
for(int i=0;i<size;i++)
cout<<num1[i]<<" ";
cout <<endl;
return 0;
}
void ballon(int arr[])
{
int i,j,temp;
for(i=0;i<size-1;i++)
for(j=i+1;j<size;j++)
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

Program Activity 6:

#include <cstring>
#include <iostream>
Error/s: 1. #include <conio> : must be #include <conio.h>
#include <conio>
2. using namespace std;

int main()
{
string str1 = "Hello";
Output:
string str2 = "World";
str1 and str2: HelloWorld
string str3;
str3: Welcome
int len;
str3 and "Philippines": Welcome Philippines

str3.length(): 19
cout<<"str1 and str2: " << str1 << str2<< endl;
Calling to find ("Phil"): 8
// copy str1 to str3
Calling substr(0,2): We
str3 = "Welcome ";
Calling substr(4): ome Philippines
cout << "str3: " << str3 << endl;
Welcome to Philippines

//concatenates str1 and "Philippines"


str3 = str3 + "Philippines";
cout << "str3 + Philippines: " << str3 << endl;

//total length of str3 after concatenation


len = str3.length();
cout << "str3.length(): " << len << endl;

//The find function will return an integer representing the FIRST


OCCURRENCE of a certain string
cout << "Calling find (\"Phil\"):" <<str3.find("Phil") << endl;

//will return a string which starts and ends at a determined index(es),


the arguments of the function. if only 1
//argument is given,it will begin at that index and continue all the
way to the end.
cout << "Calling substr(0,2): " << str3.substr(0,2) << endl;
cout << "Calling substr(4): " << str3.substr(4) << endl;

//will look at a designated index and insert the string, as the second
parameter. In that place
cout << str3.insert(8,"to ") << endl;

getch;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy