arrays in c++

Discussion in 'Meet and Greet' started by arlitch, Aug 4, 2010.

1. arlitchNew Member

Joined:
Aug 4, 2010
Messages:
1
0
Trophy Points:
0
Occupation:
student
Location:
isabel,leyte philippines
Objectives:

1. To execute inputting in a 1 dimensional array using recursion, and check if the input already occurs.
2. To implement counting and displaying of the prime numbers and numbers with n factor
Procedure:Write this program in C++

1. Declare a long int variable of array composed of 10 elements and initialize all the contents into zero.
2. Ask for 10 inputs in which each input i is 1<i<65000, then check the occurences of i in the array, if it already occurs, do not place it in the array, hence ask for another input on that same index location.
3. When all the 10 elements of the array are already filled with 10 values for each index, Implement the screen 1 below.
4. On Option 1, count and display all the prime numbers contained in the array. Goto # 7 if the user would still like to continue.
5. On Option 2, count and display all the numbers contained in the array with the factor n that you'll input, wherein 2<n<65000. Goto # 7 if the user would still like to continue.
6. If neither 1, nor 2 is pressed, repeat procedure 3.
7. Prompt the user with (Y/y or press any key to exit) if s/he would like to continue or not.
Screen # 1:

1. Count and display the number of prime numbers
2. Count and display the number of with n factor

Enter and option (either 1 or 2):

2. Cris DcstNew Member

Joined:
Aug 16, 2012
Messages:
1
0
Trophy Points:
0
Code:
```#include <iostream.h>
#include <conio.h>
void main()
{

long int array[10] = {0,0,0,0,0,0,0,0,0,0};
int i, ctr1=0, flag,k,j;
char option,o;

clrscr();
loop:
for (i=0; i<=10; i++)
{
cout<<"Enter input number(s)["<<i<<"]:";
cin>>array[i];
if (i>0) {
for (int k=i-1; k>=0; k--)
if  (array[k]==array[i]){
goto loop;
}
}
cout<<"\n";
cout<<"Enter an option (either 1 or 2):";
cin>>option;

if (option=='1')  {

cout<<"Prime number(s):\n";
for (i=0; i<=5; i++)
{
flag=0;
for (int j=2; j<=array[i]/2;j++)
{
if (array[i]%j==0)
cout<<array[i]<<" ";
{
flag=1;
break;
}
}

if (flag==0)
ctr1++;
}

cout<<array[i]<<" ";
cout<<"\nNumbers of prime numbers entered:\n"<<ctr1<<endl;
cout<<"Would you like to comtinue? y/n";
cin>>o;    }

cout<<"Program Terminated:\n" ;

}
}
```
:charming: quite easy.. hope you appreciate

Last edited by a moderator: Aug 17, 2012