Compilation Reyes

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

EXERCISE 1

This is a program that will asks the user to type 10 integers of an array. And the program must
compute and write how many integers are greater than or equal to 10.

#include <iostream>
using namespace std;
const int N=10;
int main()
{
int t[10],i,nb=0;
for(i=0;i<N;i++)
{
cout << "Type an integer: ";
cin >> t[i];
nb+= (t[i]>=10); // note that true converts to 1, false to 0
}
cout << "the number of integers greater or equal to 10 is: " << nb
<<endl;
return 0;
}

EXERCISE 2

This is a program that will asks the user to type 10 integers of an array. The program must output the
largest element in the array, and the index at which that element was found.

#include <iostream>
using namespace std;
const int MR = 10;
int main()
{
int arr[MR];
int Largest = 0, index = 0;
cout << "Please enter " << MR << " integers:" << endl;
for (int m = 0; m < MR; m++)
{
cin >> arr[m];
}
for (int m = 0; m < MR; m++)
{
if (arr[m] >= Largest)
{
Largest = arr[m];
index = m+1;
}
}
cout << "Largest number = " << Largest << ", at index: " << index << endl;
return 0;
}

EXERCISE 3
This is a program that will asks the user to type 10 integers of an array and an integer Marlyn. The
program must search if Marlyn is in the array of 10 integers. The program writes "Marlyn is in the
array" or "Marlyn is not in the array".

#include <iostream>
using namespace std;
int main(void)
{
int V,input[10];
bool equalsV(false);
cout << "Type the value of MARLYN: ";
cin >> V;
for(int m(0); m < 10; m++)
{

cout << "Enter input[" << m << "]: ";


cin >> input[m];
if (input[m] == V)
equalsV = true;
}

cout << "MARLYN is ";


if (!equalsV)
cout << "not ";
cout << "in the array" << endl;
return 0;
}

EXERCISE 4
This a program that will asks the user to type 10 integers of an array and an integer value Faith . The
program must search if the value Faith exists in the array and must remove the first occurrence of
Faith, shifting each following element left and adding a zero at the end of the array. The program
must then write the final array.

#include <iostream>
using namespace std;
const int N=10;
int main()
{
int t[N],i,j,V;
bool found;
for(i=0;i<N;i++)
{
cout << "Type an integer: ";
cin >> t[i];
}
cout << "Type the value of FAITH: ";
cin >> V;
for (i=0;i<N;i++)
if (t[i]==V)
{
for (j=i;j<N-1;j++)
t[j]=t[j+1];
t[N-1]=0;
break;
}
for(i=0;i<N;i++)
cout << t[i] << endl;
return 0;
}

//

***
#include<conio.h>
#include<stdlib.h>
#define p printf

#define s scanf
#define g getch

int main(void)
{
char ch= 'c';
char*chptr= &ch;

int i= 20;
int*intptr= &i;

float f= 1.20000;
float*fptr= &f;

char *ptr= "I am string";

p("ch is %c", *chptr);


p("\ni is %i", *intptr);
p("\nf is %.2f", *fptr);
p("\nfirst letter of the string is %c", *ptr);
p("\nthe string means %s", ptr);

/*cout<<"ch is"<< *cptr<<endl;


cout<<"i is"<< *intptr<<endl;
cout<<"f is"<< *fptr<<endl;
cout<<"first letter of the string is"<< *ptr<<endl;

cout<<"the string means" << ptr<<endl;*/


return 0;
}

This program will ask the user to enter two integers that they want to add.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define p printf

#define s scanf
#define g getch

main()
{
int num1, num2, ans;
p("Marlyn P Reyes");

p("\n\nInput two numbers:");


p("\n\nNo.1:");
s("%i",&num1);
p("\n\nNo.2:");
s("%i",&num2);
ans=num1+num2;
p("\nAnswer: %i", ans);

g();
return 0;
}

This program will ask the user to enter 5 un-order numbers then the program will
ascend the 5 numbers.
//C program for Arranging 5 Numbers in Ascending Order

#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i,j,t;

printf("Enter 5 nos.\n\n");
for (i=0;i<5;i++)
scanf("%d",&a[i]);
for (i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("Ascending Order is:");
for(j=0;j<5;j++)
printf("\n%d",a[j]);
getch();
}

/header function using pointers


//implemented in swap sort
#include<stdio.h>
#include"point.h"

int main()
{
int x=10, y=20;
printf("x=%d and y=%d",x,y);
//swap x and y
swap (&x,&y);
printf("\nx=%d and y=%d\n",x,y);

}
int swap(int *a, int *b)
{
int temp=*a;
*a=*b;
*b=temp;}

****
#include <iostream>
using namespace std;

int main() {
float celsius;
float fahrenheit;

cout << "Enter Celsius temperature: ";


cin >> celsius;
fahrenheit = (5/9) * (celsius + 32);
cout << "Fahrenheit = " << fahrenheit << endl;

return 0;

Getting the area of rectangle.


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

{
private:
int l;
int w;
public:
Area()
{
cout<<"Enter the length of the rectangle: "<<endl;
cin>>l;
cout<<"Enter the width of the rectangle: "<<endl;
cin>>w;
cout<<"The area is "<<l*w<<endl;
}
};
int main()
{
Area();
return 0;
}

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

class Conversion
{
private:

int cubicmeter;
int liter;

public:

Conversion()
{

cout<<"Enter cubic meter: "<<endl;


cin>>cubicmeter;

cout<<"cubicmeter to liter "<< cubicmeter*1000 <<endl;


}
};

int main()
{

Conversion();
return 0;
}

#include<stdio.h>
#include<stdlib.h>

int main()
{
int c, lo, hi, middle, n ,search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);

printf("Enter %d integers:",n);

for(c=0;c<n;c++)
scanf("%d",&array[c]);

printf("Enter the value to find.\n");


scanf("%d",search);

lo;
hi;
middle=(lo+hi)/2;

while(lo<=hi)
{
if(array[middle]<search)
lo=middle+1;
else if (array[middle]=search)

{
printf("%d found at the location %d.\n",search,middle+1);
break;
}
else
hi=middle-1;
middle= (lo+hi)/2;

if(lo>hi)
printf("Not found! %d is not present in the list.\n",& search);
system("pause");
return 0;
}

#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
main()
{
int a[3], i;
system("CLS");
printf("\n\t Enter three numbers : ");
for(i=0; i<3; i++)
{
scanf("%d", &a[i]); // read array
}

printf("\n\n\t Numbers are : ");


for(i=0; i<3; i++)
{
printf("\t %d", a[i]); // print array
}
getch();

This is a program that will asks the user to type 10 integers of an array. The program must output the
largest element in the array, and the index at which that element was found.

#include <iostream>
using namespace std;
const int MR = 10;
int main()
{
int arr[MR];
int Largest = 0, index = 0;
cout << "Please enter " << MR << " integers:" << endl;
for (int m = 0; m < MR; m++)
{
cin >> arr[m];

}
for (int m = 0; m < MR; m++)
{
if (arr[m] >= Largest)
{
Largest = arr[m];
index = m+1;
}
}
cout << "Largest number = " << Largest << ", at index: " << index << endl;
return 0;
}

You might also like