Totally confused...the teacher doesnt help at all..c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anan18
    New Member
    • Oct 2006
    • 11

    Totally confused...the teacher doesnt help at all..c++

    kk..there were 15 problems for for the monthy projects..i finished 11 of them..these 4 were really hard..


    5. your working on a problem in which you must display as integer ratios; therefore you need to be able to compute using common fractions and get the results that are common fractions in reduced form. you want to write a program that will allow you to add, subtract, multiply, and divide common fractions. The program will promt you for a fraction, an operator, and another fraction and then display the problem and the result. The process will be repeated until you enter an n to answer the question: continue>(y/n)


    12. A prime number is an integer that has no divisors other than 1 and itself ( for example, the integers 2,3,5,7, and 11). write a program that finds the smallest divisor of a number or determines that the number is a prime number.

    9. write a function named factor that factors its parameter, displaying the parameter and all its prime factors. For example, the function call factor(84) should display 84=2*2*3*7. Write a driver function that testes your function with prime numbers and also with numbers having from two to eight prime factors.


    10. Two positive integers n and m are considered to be relatively prime if there esist no integer greater then 1 that divides them both. Write a function relprm that has two input parameters, n and m, and returns a value of 1 for true if n and m are relatively prime. Otherwise, relprm should return a value of 0 for false. Write a driver that tests your function.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Give a more exact description of your problem in each question. Do you not understand what you need to do or do you understand the problem but are unable to create the code to solve it.

    as a tip I would do questions 9 and 10 first as the results will help in solving questions 5 and 12.

    Comment

    • Anan18
      New Member
      • Oct 2006
      • 11

      #3
      Originally posted by Banfa
      Give a more exact description of your problem in each question. Do you not understand what you need to do or do you understand the problem but are unable to create the code to solve it.

      as a tip I would do questions 9 and 10 first as the results will help in solving questions 5 and 12.

      well this is the way the teacher wrote the problems ..shes not too good in english..and im also afraid to ask her.. :/ she seems to have a short temper..but im pretty sure this stuff deals with modularity of functions..the problem is..she makes up her own problems..i tried looking and practicing some exercises from the textbook ( c++ for scientists and engineers 2nd edition, Bronson), i could not find any problem similar to these..Function and parameter declarations, returning single/multiple values are the basis of this chapter and part of the project..i understood the loops and everything else... i programmed to calculate a side and create a square using for loop, also did the same for a diamond, and chess..im ok with those stuffs...but im like totally lost in the modularity of funtions...thos e 4 problems are based on it..and im not understanding it at all..please get back to me..its due tommorow

      //Anan Khan
      //Problem 1
      //This program uses for loop to
      //create a hallow square
      #include <iostream>
      using namespace std;
      int main()
      {
      int stars,c,r;
      cout<<"Enter the length of stars:";
      cin>>stars;

      if (stars<1)
      {stars=1;
      cout<<"Invalid Input\nUsing default value 1\n";}

      else if (stars>20)
      {stars=20;
      cout<<"Invalid Input\nUsing default value 20\n";}

      for (c = 1; c <=stars; c++)
      {for (r = 1; r <=stars; r++)
      {if (c == 1 || c == stars) cout<<"*";

      else if (r == 1 || r == stars) cout<<"*";
      else cout<<" ";
      }
      cout<<endl;
      }

      }

      //Anan Khan
      //Problem 13
      //This program uses for loop to
      //create a diamond
      #include <iostream>
      using namespace std;
      int main()
      {
      int i,j,k;
      cout << " Please enter the value for k: ";
      cin >> k;

      for(i = 1; i<=k/2 + 1; i++)
      {
      for(j = -20; j<= k - i; j++){cout << " ";}

      for(j = 1; j <= 2*i - 1; j++){cout <<"*";}
      cout << endl;}

      for (i= k / 2 ; i >= 1 ; i--)
      {
      for(j = -20; j <= k - i; j++){cout << " ";}
      for(j = 1; j <= 2*i- 1; j++){cout << "*";}
      cout << endl;
      }

      return 0;

      }

      //Anan Khan
      //Problem 15
      //This program uses for loop to
      //create a a chess board and
      //all possible knight moves
      #include<iostre am>
      using namespace std;
      main()
      {
      int i,j,x,y;
      cout<<"enter the value of x & y\n";
      cin>>x>>y;
      for(i=0;i<8;i++ )
      cout<<" __";
      cout<<"\n";
      for(i=1;i<17;i+ +)
      {
      for(j=0;j<8;j++ )
      {
      if(i%2==0&&i!=( 2*y)-1)
      {
      cout<<"|__";}
      else
      if(j==x-1&&i==(2*y)-1)
      cout<<"|h ";

      else
      if((j==x+1)&&(i ==(2*(y-1))-1||i==(2*(y+1))-1))
      cout<<"|* ";
      else
      if((j==x-3)&&(i==(2*(y-1))-1||i==(2*(y+1))-1))
      cout<<"|* ";
      else
      if((j==x-2||j==x)&&(i==( 2*(y-2))-1))
      cout<<"|* ";
      else
      if((j==x-2||j==x)&&(i==( 2*(y+2))-1))
      cout<<"|* ";
      else
      {
      cout<<"| ";}
      }
      cout<<"|\n";
      }
      return 0;
      }

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        OK starting with question 9

        9. write a function named factor that factors its parameter, displaying the parameter and all its prime factors. For example, the function call factor(84) should display 84=2*2*3*7. Write a driver function that testes your function with prime numbers and also with numbers having from two to eight prime factors.

        Are you familiar with the method of spliting a number into it's prime factors?

        Comment

        • Anan18
          New Member
          • Oct 2006
          • 11

          #5
          sorry back.. i had prayer to attend..as it is Eid today.. yes i am aware of of what factorization, prime factors etc are... 2..3..5..7..11 etc..but not the method of splitting them..but if i udnerstand the question correctly do you mean for example 2=1 x 1, 3 = 2 x 1, 5= 4 x 1..etc

          Comment

          • emaghero
            New Member
            • Oct 2006
            • 85

            #6
            The following implementation should solve your prime number problem

            bool test_prime(long n);
            void list_primes(lon g n);

            int _tmain(int argc, _TCHAR* argv[])
            {
            long ub;//upper bound for the list of primes

            cout<<"Enter a number and I will list all the primes up to that number: "<<endl;
            cin>>ub;
            list_primes(ub) ;
            cout<<""<<endl;

            system("PAUSE") ;
            return 0;
            }
            bool test_prime(long n)
            {
            bool prime;
            if(n==0||n==1L)
            prime=false;
            else if (n==2L)
            prime=true;
            else if(!(n%2L))
            prime=false;
            else{
            long i_end=static_ca st<long>(0.5+ sqrt(double(n)) );
            prime=true;
            for(long i=3L;i<=i_end;i +=2L){
            if(!(n%i)){
            prime=false;
            break;
            }
            }
            }
            return prime;
            }
            void list_primes(lon g n)
            {
            cout<<"Primes up to "<<n<<" are:\n\n";
            if(n>=2)
            cout<<"2\n";
            for(long i=3L;i<=n;i+=2L )
            if(test_prime(i ))
            cout<<i<<endl;
            }

            The algorithm test_prime is based on a well known test for primality. It can be found in any book on introductory number theory

            Comment

            • Anan18
              New Member
              • Oct 2006
              • 11

              #7
              Thank you Banfa and emaghero , sorry late to reply, im really trying to understand this material. now im stuck

              i was working on this problem
              Write a function named factor that factors its parameter, displaying the parameter and all its prime factors. For example, the function call factor(84) should display 84=2 * 2 * 3 * 7.

              Write a driver that tests your function with prime numbers and also with numbers having from two to eight prime factors.

              now i got it to work up that point..then its giving me wierd numbers.. for example if i wanted to find the factors of 400, it gives me something like 2*4*5 which is incorrect. HelPP !!

              #include<iostre am>
              #include<iomani p>
              #include<cmath>
              using namespace std;


              long int factor(long int *x,long int y)
              {
              int count = 0;
              while (*x == y*(*x/y)) {
              count++;
              *x /= y;
              }
              return count;
              }

              int main()
              {
              long int i,j;


              cout<<setw(2)<< "Enter an integer to be factored: ";
              cin>>i;
              cout<<setw(2)<< "The prime factors of"<<setw(3)< < i <<" are:\n";
              if (i==2*(i/2))
              cout<<" 2 * "<<factor(&i,2) ;
              j = 3;
              while(i > 1) {
              if (i==j*(i/j))
              cout<<setw(2)<< " * "<<j,factor(&i, j);
              j+=2;

              }
              }

              Comment

              Working...