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. Create a block diagram of the algorithm.
2. source code
3. 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!!!!
Last edited on
Hello vask0,

It has been my experience that a post like yours tends to be ignored because most people here will not do your work for you.

Write some code and post it, point out where you are having a problem and then you can get some help.

I will suggest that "db", "V1" and "V2"be defined as doubles because of the division and you will need "db" as a floating point number. Also I would check that "V1" is not zero because of the divide by zero problem you would eventually come to.

You may also want to research how to calculate "db". I think your formula that you posted is a little light.

Hope that helps,

Andy
Hello Andy, thank you for the advice. I give the code, but I do not know if it's true?

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#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);
}



10x !!!
Last edited on
Hello vask0,

This is a very nice program even great full that it compiles with no errors, but what does it have to do with figuring the dB of a given input?

Unless I am entering the wrong values for "V1" and "V2" I believe that you are using the wrong subscripts for lines 59 and 60.

If there is some test data that you are using that has a known result it is helpful to include that with you code so that a known input will produce a known output.

Hope that helps,

Andy
Hi andy, you definitely help me, but lines 50 and 60 with what I would replace them and if only they are wrong with the condition I gave. Оr I need to change the whole code if I want to perform the functions of the topic.

thanks!!!
Hello vask0,

From your OP it says:
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
Your program is in totally different direction.

If this is what you want to use you will have to tell me what the (graph/table/array) is for and how you want to use it? Using "v1" and "v2" as your subscript for the array could put an element outside the boundaries of the array. This would not good and would have unknown results.

You have me at a loss, because I in-visioned a much different program that did not include an array or having to store the results of the calculation.

A possibility would be to store the results of the calculation of dB in the array and later use those numbers to print something out. Just a guess at this point.

While I am thinking about it, what is
2. Write sorcid
I do not understand what "sorcid" is? If this in not a language difference then it is something I have not seen before and I am not sure what it means.

Andy
2. Write sorcid this is my fault...

I meant the source code.


that is, I only need the source code of the program.
Last edited on
some ideas..?
@vask0

The code that you have presented has nothing remotely to do with the statement of your problem.

It appears to be something you have cut-and-pasted from elsewhere to try to demonstrate that you have made some attempt at the problem. In fact it appears to be this one:
http://www.sanfoundry.com/cpp-program-represent-graph-using-2d-array/
but there are several other sources on the web.

I don't think that is very clever.

Please present a genuine attempt of your own at solving your particular problem. Then someone might offer assistance.
here's what I figured out but it returns my mistake.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#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;
}




Topic archived. No new replies allowed.