Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    11
    Rep Power
    0

    Problem under C++


    Hello fellow I have the following problem to solve ..

    I will be grateful to someone who can help with the solution ..

    Write a program that inputs values V1, V2 from the keyboard and calculates the ratio of V1 / V2 in dB by the formula
    dB = 20 lg (V2 / V1) for V2> 0; V1> 0


    1. Write source code C++
    2. Test it

    Verification
    A) Example Input V2 / V1> 1
    B) Example Input V2 / V1 <1
    C) Example input V2 = V1
    F) Example output dB> 0
    F) Example output dB <0
    D) Example output dB = 0

    10x!!!!
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,898
    Rep Power
    509
    For A I'd think that V2 = 1e6 and V1 = 1e-6 ought to work.
    And for B you could set V2 to 1e-6 and V1 = 1e6 .
    C. Quite difficult. I cannot think this through. How about V1 = V2 = 0; // ?
    The answer to F could be the same as the answer to A.
    The answer to F ought to be same as the answer to B.
    Yes I know I wrote that.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    11
    Rep Power
    0
    Hi colleague and thank you for your reply ..
    source-code how would you look? How about V1 = V2 = 0; // ?... you idea sounds good

    and we have to get him to work.
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,898
    Rep Power
    509
    and I'll mention for D) you'd need to solve lg(x) = 0 for x and then find values for V1 and V2 for which x = V2/V1 could be true. A difficult task. Here's the verb in j, you'll just need to translate it to c.
    Code:
       NB. www.jsoftware.com Ken Iverson's final dialect of APL
       dB=: 20 * 10 ^. %  NB. use:  V2 dB V1
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    11
    Rep Power
    0
    Originally Posted by b49P23TIvg
    and I'll mention for D) you'd need to solve lg(x) = 0 for x and then find values for V1 and V2 for which x = V2/V1 could be true. A difficult task. Here's the verb in j, you'll just need to translate it to c.
    Code:
       NB. www.jsoftware.com Ken Iverson's final dialect of APL
       dB=: 20 * 10 ^. %  NB. use:  V2 dB V1


    Hi colleague again, I think the code should be something of the sort

    Code:
    #include<iostream>
    #include<iomanip>
     
    using namespace std;
     
    // A function to print the matrix.
    void PrintMat(int **mat, int n)
    {
    	int i, j;
     
    	cout<<"\n\n"<<setw(4)<<"";
    	for(i = 0; i < n; i++)
    		cout<<setw(3)<<"("<<i+1<<")";
    	cout<<"\n\n";
     
    	// Print 1 if the corresponding vertexes are connected otherwise 0.
    	for(i = 0; i < n; i++)
    	{
    		cout<<setw(3)<<"("<<i+1<<")";
    		for(j = 0; j < n; j++)
    		{
    			cout<<setw(4)<<mat[i][j];
    		}
    		cout<<"\n\n";
    	}
    }
     
    int main()
    {
    	int i, v, e, j, v1, v2;
     
    	// take the input of the number of edges.
    	cout<<"Enter the number of vertexes of the graph: ";
    	cin>>v;
     
    	// Dynamically declare graph.
    	int **graph;
    	graph = new int*[v];
     
    	for(i = 0; i < v; i++)
    	{
    		graph[i] = new int[v];
    		for(j = 0; j < v; j++)graph[i][j] = 0;
    	}
     
    	cout<<"\nEnter the number of edges of the graph: ";
    	cin>>e;
     
    	// Take the input of the adjacent vertex pairs of the given graph.
    	for(i = 0; i < e; i++)
    	{
    		cout<<"\nEnter the vertex pair for edge "<<i+1;
    		cout<<"\nV(1): ";
    		cin>>v1;
    		cout<<"V(2): ";
    		cin>>v2;
     
    		graph[v1-1][v2-1] = 1;
    		graph[v2-1][v1-1] = 1;
    	}
     
    	// Print the 2D array representation of the graph.
    	PrintMat(graph, v);
    }
    But this is another very different task...
  10. #6
  11. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,898
    Rep Power
    509
    Absolutely correct. The solution to this puzzle will have similarities to the program you posted. Here's the lg function for you:
    Code:
    double lg(double a) {
      const double MAGIC_NUMBER = 2.302585092994045901;
      const int LUCKY_SEVEN = 7;
      double sum = 0, x = a-1;
      int i;
      for (i = LUCKY_SEVEN; i; --i) {
        sum += 1.0/(i*(1-((!(i&1))<<1)));
        sum *= x;
      }
      return sum / MAGIC_NUMBER;
    }
    Last edited by b49P23TIvg; November 6th, 2017 at 10:24 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    11
    Rep Power
    0
    hello colleague and thank you very much for your answers and help.
    the final code should look like ..?

    Code:
    #include<iostream>
    #include<iomanip>
     
    using namespace std;
    
    double lg(double a) {
      const double MAGIC_NUMBER = 2.302585092994045901;
      const int LUCKY_SEVEN = 7;
      double sum = 0, x = a-1;
      int i;
      for (i = LUCKY_SEVEN; i; --i) {
        sum += 1.0/(i*(1-((!(i&1))<<1)));
        sum *= x;
      }
      return sum / MAGIC_NUMBER;
    }
  14. #8
  15. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,898
    Rep Power
    509
    Yes that's great now just compile it into an executable and run it.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    11
    Rep Power
    0
    Hi colleague I was trying to work, but it returns me the following jar

  18. #10
  19. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,898
    Rep Power
    509
    gosh maybe the
    Code:
    int main() {
      return 0;
    }
    was one of the similarities we agreed upon beforehand. Anyway, congratulations. I was fairly certain you'd be unable to use the compiler in any way whatsoever. Score one for you.
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    11
    Rep Power
    0
    [code]

    #include <iostream>
    #include <windows.h>

    using namespace std;

    double lg(double a) {
    const double MAGIC_NUMBER = 2.302585092994045901;
    const int LUCKY_SEVEN = 7;
    double sum = 0, x = a-1;
    int i;
    for (i = LUCKY_SEVEN; i; --i) {
    sum += 1.0/(i*(1-((!(i&1))<<1)));
    sum *= x;
    int main() {
    return 0;
    }


    [code]

    This code does not work...
  22. #12
  23. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,898
    Rep Power
    509
    Remarkable. Maybe it's because you haven't applied the lg function to any of the ratios identified in post 2. Then again, there could be additional problems.
    [code]Code tags[/code] are essential for python code and Makefiles!
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    11
    Rep Power
    0
    will you be able to provide sample code to test if it works

    10x
  26. #14
  27. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,898
    Rep Power
    509
    oh sure here's a program to test your superb decibel function.
    Code:
    //
    // test program for the decibel function provided by someone else.
    // main returns the number of errors discovered.
    //
    // exercise for reader: extend to include tests for special case values
    // of V1 and V2.  These depend on how you choose to handle them.
    //
    
    #include<iostream>
    
    #define DIM(A) (sizeof(A)/sizeof(*(A)))
    #define ABS(A) ((A) < 0 ? -(A) : (A))
    #define MIN(A, B) ((A) < (B) ? (A) : (B))
    
    struct {
      double V2, V1, dB_expected;
    } test[] = {
      {0.100,   9.600,    -39.6454246607913632},
      {0.600,   9.100,    -23.6178028387490002},
      {1.100,   8.600,    -17.8621153217068525},
      {1.600,   8.100,    -14.0873007244544990},
      {2.100,   7.600,    -11.1718859509374404},
      {2.600,   7.100,     -8.7257000149651454},
      {3.100,   6.600,     -6.5636448341519182},
      {3.600,   6.100,     -4.5805466848695939},
      {4.100,   5.600,     -2.7080834057292984},
      {4.600,   5.100,     -0.8962468883272455},
      {5.100,   4.600,      0.8962468883272461},
      {5.600,   4.100,      2.7080834057292993},
      {6.100,   3.600,      4.5805466848695939},
      {6.600,   3.100,      6.5636448341519182},
      {7.100,   2.600,      8.7257000149651454},
      {7.600,   2.100,     11.1718859509374404},
      {8.100,   1.600,     14.0873007244544972},
      {8.600,   1.100,     17.8621153217068525},
      {9.100,   0.600,     23.6178028387489967},
      {9.600,   0.100,     39.6454246607913632},
    };
    
    int tolerant_equal(double a, double b, double relerr, double abserr) {
      double abserris = ABS(a - b);
      return (abserris < MIN(ABS(a), ABS(b)) * relerr) || (abserris < abserr);
    }
    
    // supply the function decibel.  It is passed the arguments V2 and V1 in that order.
    double decibel(double, double);
    
    int main() {
      double V1, V2, expect, got;
      unsigned i, errors = 0;
      for (i = 0; i < DIM(test); ++i) {
        V1 = test[i].V1;
        V2 = test[i].V2;
        expect = test[i].dB_expected;
        got = decibel(V2, V1);
        if (! tolerant_equal(got, expect, 1e-5, 1e-4)) {
          ++errors;
          std::cout << "decibel(" << V2 << ", " << V1 << ") gave " << got << " expected " << expect << '\n';
        }
      }
      return errors;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2017
    Posts
    11
    Rep Power
    0
    Hi colleague this code worked ... thank you very much !!!
    look for me in skype: linux_bg-

    10x!!!!
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo