0

i am doing a C++ program that gets number of vectors, so user can input magnitude of each vector. using 3 functions
1.main function, 2.function that get the number of vectors(looping-number of vector must be positive) and 3.last function get the magnitude of vectors (looping - must be positive) and those input values should returns to the main function for calculation. how do u do that??

int main ()
{
	int number_vectors;
	int vectors;

	cout << "VECTOR SUM\n";
		
	for (int count = 0; count <= number_vectors; count ++)
	{
                  cout << "Enter number of vectors: ";
		cin >> number_vectors;
                  number_vectors = get_vectors (vectors);

	}
	return 0;
}

int get_vectors (int vectors)
{
	while (vectors <= 0);
		{
			cout << "Number must be greater than 0.\n";
		}
		return (vectors);
}

Edited by Narue: added code tags

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by xsnake7
0

example of input:
Enter number of vectors: 0
number must be greater than 0
Enter number of vectors: 2
enter magnitude of vector 1:
enter magnitude of vector 2:

0

hey there ... i am trying to this as well .... here is where i am up to ...

#include <iostream>
#include <cmath>
using namespace std;
double pi = 3.14159265358979323846264;
int z =0.0;
double M =0.0;
double D =0.0;
double x;
double y;

int number_of_vectors()
{
	int v;
	cout << "Enter number of vectors : ";
	cin >> v;
	while ( v<=0)
	{
		cout << "Number must be greater than zero.\n";
		cout << "Enter number of vectors : ";
	    cin >> v;
	}
	z = v;
	return z;
}
double magnitude()
{
	double m;

	for (int n = 1; n <= z; n++)
	{
	   cout <<"Enter magnitude of vector "<< n <<" : ";
	   cin >> m;
	   while (m < 0 )
	   {
		   cout << "Magnitude cannot be negative\n";
		   cout <<"Enter magnitude of vector "<< n <<" : ";
	       cin >> m;
	   }
	   M=m;
	   x = M*cos(pi*D/180);
	}
    return x;
}

double direction()
{
	double d;
	for (int n = 1; n<= z; n++)
	{
		cout <<"Enter direction of vector "<< n <<" : ";
		cin >> d;
		while (d < - 180 || d > 360)
		{
			cout <<"Direction must be -180 to + 360\n";
			cout <<"Enter direction of vector "<< n <<" : ";
		    cin >> d;
		}
		D = d;
		y = M*sin(pi*D/180);
	}
	
	return D;
}

int main()
{
	cout <<"VECTOR SUM\n";
	number_of_vectors();
	magnitude();
	direction();
	double m;
	double o;
	m = sqrt((x*x) + (y*y));
	o = atan2(y,x) * 180/pi;
	cout << "Total vector :\n";
	cout << "magnitude = "<< m <<"\n";
	cout << "direction = "<< o <<" degrees.\n";
	int k;
	cin >>k;
	return 0;
}

the problem is that i want to sum whatever magnitudes and directions i get from the magnitude and direction functions and a little bit different presentation as i want it to ask for vector 1 magnitude then direction then vector 2 magnitude and direction and so on.... please help

0

bin2207
i think what you need is up to the end of magnitude function in my coding then you go to the main function and delete everything not related to whatever you need...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.