| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Wierd problem!
Hey all, I ran the code below , and it works just fine till the very end where dos says "hit any key to exit" (something like that).
THEN IT CRASHES MY COMPUTER!!!!!!!! I can't find the problem in it, though I suspect it's a pointer problem. Can someone please help me out! [the purpose of the program is to make the vector dot product, if some1 wants to see if the calculations are correct also, I would be pleased and grateful] **THis program seems to only crash WIN ME in a blue screen way, other windows versions make the program disappear #include <iostream> #include <stdlib.h> #include <math.h> #include <fstream> //ALLOCATE MEMORY using namespace std; //starting code for hwc struct vector {int dimen; double *x, mag;}; void loadvec(vector &vec) //use cin to read vector data { //ask user for dimension and vector elements cout << "Enter vector dim: "; cin >> vec.dimen; cout<<"\nEnter Vector elements:"; double temp=0; for(int i=0;i<vec.dimen;i++){ cin>>vec.x[i]; temp=(temp+(vec.x[i]*vec.x[i]));} vec.mag=sqrt(temp); //calculate the magnitude of the vector here too, sqrt of sum of sqrs } double dotprod(vector vec1, vector vec2) {//right by other double dp = 0; for (int i =0; i < vec1.dimen; i++) dp = dp + vec1.x[i]*vec2.x[i]; //calculate the dot product return dp; } void vecprint(vector vec) { for (int i = 0; i < vec.dimen; i++) cout << vec.x[i] << endl; } int main(int argc, char *argv[]) {vector vec1,vec2; //ask user for 2 vectors using loadvec loadvec(vec1);loadvec(vec2); //print out dot product and magnitude of each vector cout << "Input vectors are :\n"; vecprint(vec1); cout <<"and \n"; vecprint(vec2); cout << "Magnitude is: vec1:"<<vec1.mag<<" vec2:"<<vec2.mag<<endl; cout<< "The dot product = " << dotprod(vec1, vec2) << endl; //be sure to delete allocated memory //delete...... //Also, write the results to a file given on the command line. //ofstream outfile(agrv[1]); system("PAUSE"); return 0; } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Wierd problem! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|