//This program is intended to demonstrate simple serial programming where a vectors element gets modified serially. // #include <iostream> #include <vector> #include <cmath> using std::cout; using std::endl; using std::vector; void increase_magnitude(float *starting_address,unsigned int size_vec, float mag_multiplyer) { //the variable *starting_address is a pointer which will point to (or contain address of), the first element of the array. for (int i=0;i<size_vec;i++) { //let us multiply the whole vector by 2 //since we have address of first element of vector, we can take its value by use of * *starting_address=*starting_address * 2; //now we shall increase its address by 1. starting_address+=1; } //in this way at the same memory location we will have modified the vector. } float magnitude_finder(float *starting_address_vec,unsigned int size) { //This function will return the magnitude of the vector. float sum=0.0; for (int i=0;i<size;i++) { sum+=pow((*starting_address_vec),2); } return pow(sum,0.5); } int main(int argc, char *argv[]) { unsigned int N=1<<31; //the number implies 31 zeroes in front of 1. So it is 2^31. vector<float> vec(N); //whenever we declare an vector, it has undefined size(if we don't provide the size). Unless we declare it with some size. //declaring vector of finite size; vector<int> v(100); /* vector<int>v1(100); */ //declaring vector without size; vector<int> vec; //let us define a pointer to integer vector //let us fill the vector with N natural numbers(1,2,.......N). cout<<N<<endl; for (int i=0;i<N;i++) { vec[i]=i; } float *ptr_to_vec=&vec[0]; //giving address of first element. Or we can just write: //we shall modify the vector by multiply it with some real number. //Real numbers are stored in float data types(require 4byte per real number) and double data types(8bytes). float multiplier=4.0; //Let us find the magnitude before changing the vector, float mag_before=magnitude_finder(ptr_to_vec,N); increase_magnitude(ptr_to_vec,N,multiplier); float mag_after=magnitude_finder(ptr_to_vec,N); float ratio_of_magnitudes=mag_after/mag_before; cout<<"the final vector has magnitude "<<ratio_of_magnitudes<<" times the earlier one"<<endl<<"which should be equal to "<<pow(multiplier,0.5)<<endl; return 0; }
```
Output:
2147483648
the final vector has magnitude -nan times the earlier one
which should be equal to 2
```
Remarks: The magnitude can't handle large numbers in float data type, one must use double or long double.