arrays in c++

arlitch's Avatar, Join Date: Aug 2010
Newbie Member
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):


help me please!!!!!
Cris Dcst's Avatar, Join Date: Aug 2012
Newbie Member
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]){
                                cout<<"The input value already occurs!\n";
                                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" ;

}
}
quite easy.. hope you appreciate

Last edited by shabbir; 17Aug2012 at 14:56.. Reason: Code blocks