So I was doing a program where i need to find the total area of all floors, where the floors are determined by the user. I think I'm correct on the part to use the pointers, so to check if the code was correct I tried it doing it with basic addition. but even with basic addition it seems to have already some problems. I tried looking for similar questions in here and I can't find anything that might help me, so I hope you guys can help me. Thank you in advance.
float *Length=NULL, *Width=NULL, *Area=NULL, TotalArea, templ, tempw;
int floors, count;
cout << "Input the number of floors to proceed\n";
cout << ":";
cin >> floors;
Length = new float[floors];
Width = new float[floors];
Area = new float[floors];
for (int loop = 0; loop < floors; loop++)
{
cout << "\n\nFloor " << loop + 1 << endl;
cout << "Input the Length: \n";
cin >> templ;
cout << "Input the Width: \n";
cin >> tempw;
*(Length + loop) = templ;
*(Width + loop) = tempw;
*(Area + loop) = (*Length + loop) + (*Width + loop);
count = loop;
for (int count = 0; count < floors; count++)
{
TotalArea = TotalArea + *Area+count;
}
}
cout << TotalArea << endl;
I tried inputting the following:
floor:2
floor 1
length: 1
width: 1
floor 2
length: 1
width: 1
The answer should be 4, but the output ends up with 10.
You don't need arrays and pointers. Since you are looping through the floors and summing the areas of each floor, you can "forget" about previously encountered lengths, widths and areas, and only remember the total area encountered so far.
float length;
float width;
float area;
float totalArea;
int floors;
std::cout << "Input the number of floors to proceed\n";
std::cout << ":";
std::cin >> floors;
totalArea = 0;
for (int loop = 0; loop < floors; loop++)
{
std::cout << "\n\nFloor " << loop + 1 << std::endl;
std::cout << "Input the Length: " << std::endl;
std::cin >> length;
std::cout << "Input the Width: " << std::endl;
std::cin >> width;
area = length * width;
totalArea += area;
}
std::cout << totalArea << std::endl;
Notice how I added these annoying std:: everywhere? If you wonder why, see this other question: Why is “using namespace std;” considered bad practice?
I tried making some changes based on you guys comments and here is what I did.
There are some warnings I don't really know why but It seems to be working
float* Length = NULL, * Width = NULL, TotalArea, tempw, templ, * Area = NULL;
int floors, loop = 0;
cout << "Input the number of floors to proceed\n";
cout << ":";
cin >> floors;
for (int loop = 0; loop < floors; loop++)
{
Length = new float[floors];
Width = new float[floors];
Area = new float[floors];
cout << "\n\nFloor " << loop + 1 << endl;
cout << "Input the Length: \n";
cin >> templ;
cout << "Input the Width: \n";
cin >> tempw;
*(Length +loop) = l;
*(Width+loop) = w;
Area[floors] = (*(Length+loop)) * (*(Width+loop));
TotalArea += a[floors];
}
cout << TotalArea << endl;
This is what I came u with, it seems to be working, the output is also correct now.
Related
#include <iostream>
using namespace std;
void main()
{
int numDays;
double sum = 0;
double avg;
cout << "Enter the number of days of sales";
cin >> numDays;
double *Sales = new double[numDays];
for (int i = 0; i < numDays; i++)
{
cout << "enter how much you sold for day " << i << endl;
cin>>*Sales;
sum = sum + *Sales;
cout << Sales;
}
delete[] Sales;
avg = sum / (numDays);
cout << "the sum is" << sum << endl;
cout << "the avg is" << avg << endl;
}
Hi this is the output I'm getting can somone explain why the pointer doesn't need to be incremented? and the proper way of doing the same task with pointers.
Enter the number of days of sales2
enter how much you sold for day 0
1
0050CD70enter how much you sold for day 1
2
0050CD70the sum is3
the avg is1.5
Press any key to continue . . .
There are a couple styles for doing this. You can either index into the array itself:
double *Sales = new double[numDays];
for (int i = 0; i < numDays; i++)
{
cout << "enter how much you sold for day " << i << endl;
cin >> Sales[i];
sum = sum + Sales[i];
}
Or you can advance a pointer, making sure to keep a copy of the original pointer. In the next example, the destination of p creeps along indices of Sales. (Make sure not to use delete on p since it wasn't created with a new!)
double *Sales = new double[numDays];
double *p = Sales;
for (i = 0; i < numDays; i++)
{
cout << "enter how much you sold for day " << i << endl;
cin >> *(p++);
}
(Technically the parentheses aren't needed in *(p++), and in fact there's a lot of code that uses *p++ instead).
How would I modify this code so that I can enter in more than just one coefficient?
I am suppose to be able to enter in something like "3 2 1" and the spaces shouldn't affect my user input but I'm not sure how to do this.
This is the code I have so far:
#include <iostream>
using namespace std;
int *foil(int A[], int B[], int co, int coo)
{
int *product = new int[co+coo-1];
for (int i = 0; i<co+coo-1; i++)
product[i] = 0;
for (int i=0; i<coo; i++)
{
for (int j=0; j<co; j++)
product[i+j] += A[i]*B[j];
}
return product;
}
void printPoly(int poly[], int co)
{
for (int i=0; i<co; i++)
{
cout << poly[i];
if (i != 0)
cout << "x^" << i ;
if (i != co-1)
cout << " + ";
}
}
int main()
{
int co, coo;
int *A;
A=new int[co];
int *B;
B=new int[coo];
cout << "How many coefficients are in the first poly?: ";
cin >> co;
cout << "What are the coefficients? (Lowest power first): ";
cin >> *A;
cout << "How many coefficients are in the second poly?: ";
cin >>coo;
cout << "What are the coefficients? (Lowest power first): ";
cin >>*B;
printPoly(A, coo);
cout << "\n";
cout << "times" << endl;
printPoly(B, co);
cout << "\n";
cout << "-----" << endl;
int *product = foil(A, B, co, coo);
printPoly(product, co+coo-1);
return 0;
}
It outputs this:
How many coefficients are in the first poly?: 3
What are the coefficients? (Lowest power first): 3 2 1
How many coefficients are in the second poly?: What are the coefficients? (Lowest power first): 3 + 0x^1
times
1 + 0x1 + 0x^2
-----
3 + 0x^1 + 0x^2 + 0x^3
I want it to output it like this:
How many coefficients are in the first poly?: 3
What are the coefficients? (Lowest power first): 3 2 1
How many coefficients are in the second poly? 3
What are the coefficients? (Lowest power first): 1 2 1
3 + 2x^1 + 1x^2
times
1 + 2x^1 + 1x^2
-----
3 + 8x^1 + 8x^2 + 4x^3 + 1x^4
you need to take the inputs in a loop.
here is how you can do it.
cout << "How many coefficients are in the first poly?: ";
cin >> co;
cout << "What are the coefficients? (Lowest power first): ";
for(int i=0;i<co;i++)
cin >> *(A+i);
cout << "How many coefficients are in the second poly?: ";
cin >>coo;
cout << "What are the coefficients? (Lowest power first): ";
for(int i=0;i<coo;i++)
cin >>*(B+i);
printPoly(A, coo);
cout << "\n";
cout << "times" << endl;
printPoly(B, co);
cout << "\n";
cout << "-----" << endl;
int *product = foil(A, B, co, coo);
printPoly(product, co+coo-1);
getch();
return 0;
Here is the output image
I want to repetitively solve the CG/BicGSTAB using CUSP solver, called from Fortran. To avoid transfers I am passing the Fortran data directly to CUSP. The code compiles but breaks at the run time flagging:
terminate called after throwing an instance of 'thrust::system::system_error'
what(): invalid argument
terminate called recursively
Aborted (core dumped)
Let alone the core of the code, even the print stream is not happening. The code of course is in the preliminary stage, but I wonder what is wrong with it.
extern "C" void bicgstab_(int *device_I, int *device_J, float *device_V, float *device_x, float *device_b, int *n, int *nnz){
int N = *n;
int NNZ = *nnz;
std::cout << N << " " << NNZ << " " << *device_I << std::endl;
for(int i=0; i<N;i++)std::cout << device_I[i] << " "; std::cout << std::endl;
for(int i=0; i<NNZ;i++)std::cout << device_J[i] << " "; std::cout << std::endl;
for(int i=0; i<NNZ;i++)std::cout << device_V[i] << " "; std::cout << std::endl;
for(int i=0; i<N;i++)std::cout << device_x[i] << " "; std::cout << std::endl;
for(int i=0; i<N;i++)std::cout << device_b[i] << " "; std::cout << std::endl;
// *NOTE* raw pointers must be wrapped with thrust::device_ptr!
thrust::device_ptr<int> wrapped_device_I(device_I);
thrust::device_ptr<int> wrapped_device_J(device_J);
thrust::device_ptr<float> wrapped_device_V(device_V);
thrust::device_ptr<float> wrapped_device_x(device_x);
thrust::device_ptr<float> wrapped_device_b(device_b);
// use array1d_view to wrap the individual arrays
typedef typename cusp::array1d_view< thrust::device_ptr<int> > DeviceIndexArrayView;
typedef typename cusp::array1d_view< thrust::device_ptr<float> > DeviceValueArrayView;
std::cout << wrapped_device_I[3];
/*
DeviceIndexArrayView row_indices (wrapped_device_I, wrapped_device_I + (N+1));
DeviceIndexArrayView column_indices(wrapped_device_J, wrapped_device_J + NNZ);
DeviceValueArrayView values (wrapped_device_V, wrapped_device_V + NNZ);
DeviceValueArrayView x (wrapped_device_x, wrapped_device_x + N);
DeviceValueArrayView b (wrapped_device_b, wrapped_device_b + N);
// std::cout << device_x[0] ;
// for(int i=0;i<NNZ;i++)std::cout << column_indices[i] << std::endl;
// combine the three array1d_views into a csr_matrix_view
typedef cusp::csr_matrix_view<DeviceIndexArrayView,
DeviceIndexArrayView,
DeviceValueArrayView> DeviceView;
// construct a csr_matrix_view from the array1d_views
DeviceView A(N, N, NNZ, row_indices, column_indices, values);
// set stopping criteria: // iteration_limit = 100 // relative_tolerance = 1e-5
cusp::verbose_monitor<float> monitor(b, 100, 1e-5);
// solve the linear system A * x = b with the Conjugate Gradient method
// cusp::krylov::bicgstab(A, x, b);*/
}
If this is not feasible, I can move over to another approach,but as I am not sure about the correctness, I am unable to decide. Any help is appreciated.
I'm supposed to find the minimum and maximum values in the array, but I can't seem to figure out why the answers aren't correct. For example if I entered "1 2 3 4 5" as my five times, it told me 1 was my maximum and 0 was the minimum. For some reason, whatever the first number is, it calls it the max and it also assigns 0 as the min.
#include <iostream>
using namespace std;
int find_distance(int j); //a function that returns a distance based on the choice j
int intmax, intmin;
int main( )
{
int i =0;
int distance[6];
double data[6][5];
for(int j = 0; j < 6; j++)
{
distance[j] = find_distance(j);
cout << "\nEnter 5 of your best running times for \n " << distance[j] << " m \n";
for(int i = 0; i < 5; i++)
{
cout << "Enter a time \n"; cin >> data[j][i];
}
}
cout << "Here is your best 5 times: ";
for(int j = 0; j < 6; j++)
{
cout << "\nDistance : " << distance[j] << " m \n";
for(int i = 0; i < 5; i++)
{
system ("pause");
cout << data[j][i] << "\t"; } cout << endl;
if (data[j][i] < intmin)
intmin = data[j][i];
else if (data[j][i] > intmax)
intmax = data[j][i];
cout << "The maximum time is: " << intmax << endl;
cout << "The minimum time is: "<< intmin << endl;
}
return 0;
}
int find_distance(int j)
{
switch (j)
{ case 0: // 100 meter
return 100;
break;
case 1: // 150 meter
return 150;
break;
case 2: // 200 meter
return 200;
break;
case 3: // 400 meter
return 400;
break;
case 4: // 500 meter
return 800;
break;
default: // 1600 meter
return 1600;
}
}
The minimum value is 0 because when you initialize intmin, it is set to 0 by default. You never enter a negative time, so in your comparisons it is always less than the compared value.
The maximum value is off because your for loop ends in an odd place and the comparison code is improperly executed. Change this code:
for(int j = 0; j < 6; j++)
{
cout << "\nDistance : " << distance[j] << " m \n";
for(int i = 0; i < 5; i++)
{
system ("pause");
cout << data[j][i] << "\t"; } cout << endl; //why does the for loop end here?
if (data[j][i] < intmin)
intmin = data[j][i];
else if (data[j][i] > intmax)
intmax = data[j][i];
//move the end bracket to this line and it should work
cout << "The maximum time is: " << intmax << endl;
cout << "The minimum time is: "<< intmin << endl;
}
Just to practice:
#include <iostream>
#include <algorithm>
#include <string>
#include <boost/regex.hpp>
int main () {
using namespace std;
string input;
boost::regex re("-?\\d+");
vector<int> integers;
cout << "enter sequence of integers: ";
getline(cin, input);
boost::sregex_token_iterator begin(input.begin(), input.end(), re, 0);
boost::sregex_token_iterator end;
while (begin != end) {
integers.push_back(stoi(*begin));
++begin;
}
if (integers.size()) {
auto pair = minmax_element(integers.begin(), integers.end());
cout << "min: " << *pair.first << " max: " << *pair.second << endl;
} else {
cout << "you didn't enter any integers." << endl;
}
return 0;
}
This is how to compile and to run:
$ g++ -o lab_2 -std=c++11 -lboost_regex lab_2.cpp
$ ./lab_2
$ enter sequence of integers: -10 34 75 101 2 43
$ min: -10 max: 101
Requires boost installed because STL regular expressions aren't functional yet.
Trying to display # of each combo ordered and total price. Not sure why it won't store values in A, B, and C. Novice programmer here, so be easy. Been having the problem with if statements for a while, so obviously I'm doing the whole if statement thing incorrectly.
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
char combo = ' ';
int A = 0;
int B = 0;
int C = 0;
double total = 0.0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter = counter + 1)
{
cout << "Enter combo ordered: ";
cin >> combo;
if (combo = A)
{
A = A + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
else if (combo = B)
{
B = B + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
else if (combo = C)
{
C = C + 1;
cout << "Enter combo ordered: ";
cin >> combo;
}
total = A*6 + B*6.25 + C*5.75;
}
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
For-loop should be for (int counter = 0; counter < group; counter++)
If statement should use == for equality. = is for assignment only.
Your if statement needs to quote the character you are comparing: if (combo == 'A') {. It is also possible that you'll need to access combo as an array of characters, like so: if (combo[0] == 'A') {
I think you may need to tweak just a few things; a few compilers don't like it when you compute a double using int and there is no reason to not use double here since the program is small. Also a few syntax errors (i.e. with = instead of ==). Have you tried displaying your output in an isolated way? Something like:
main(){
double A = 1;
double B = 2;
double C = 3;
double total = A*6 + B*6.25 + C*5.75;
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
Your corrected code:
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
char combo = ' ';
double A = 0;
double B = 0;
double C = 0;
double total = 0.0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter++)
{
cout << "Enter combo ordered: ";
cin >> combo;
if (combo == 'A')
{
A++;
}
else if (combo == 'B')
{
B++;
}
else if (combo == 'C')
{
C++;
}
cout << "Enter combo ordered: ";
cin >> combo;
}
total = A*6 + B*6.25 + C*5.75;
cout << "# of Combo A ordered: " << A << endl;
cout << "# of Combo B ordered: " << B << endl;
cout << "# of Combo C ordered: " << C << endl;
cout << "Total price: $" << total << endl;
system("pause");
return 0;
}
I would also display your value for group to make sure that the if loop is even running at least once. There are a few fault points here that I would test individually.
EDIT:
Maybe test this:
#include <iostream>
using namespace std;
int main( )
{
int group = 0;
cout << "How many customers are in the group? ";
cin >> group;
for (int counter = 0; counter < group; counter++)
{
cout << "Test success";
}
to see if you are even entering into your for loop.