| | |
to calculate square and cube form given input using arrays..
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 32
Reputation:
Solved Threads: 0
Hello, please help me I am trying to write program that allows a user to enter 10 numbers, stores in an array and then displays each with it's square (no * no) and cube (no * no * no), I am able to get square but cube in not working, it gives me output my that not cube for the number please help me to solve this also in output I want user input number, square and cube the below is my code.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int size = 10;
void square_array (double values [size]);
void read_numbers (double values [size]);
void print_cube_root (double values [size]);
int main(int argc, char *argv[])
{
const int size = 10;
double number;
double values [size];
read_numbers (values);
square_array (values);
print_cube_root (values);
cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
void read_numbers (double values [size])
{
int index;
cout << "Enter 10 numbers:\n";
for (index = 0; index < size; index++)
cin >> values [index];
}
void square_array (double values [size])
{
int index;
cout << "\nThe square of the numbers are:\n";
for (index = 0; index < size; index++)
{
values [index] = values [index] * values [index];
cout << "\n\n";
cout << values [index];
}
}
void print_cube_root (double values [size]) //this part is not //worknig
{
int index;
cout << "\n\nThe cube root of the numbers are:\n";
for (index = 0; index < size; index++)
{
values [index] = values [index] * values [index];
cout << "\n\n";
cout << values [index];
}
}
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int size = 10;
void square_array (double values [size]);
void read_numbers (double values [size]);
void print_cube_root (double values [size]);
int main(int argc, char *argv[])
{
const int size = 10;
double number;
double values [size];
read_numbers (values);
square_array (values);
print_cube_root (values);
cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
void read_numbers (double values [size])
{
int index;
cout << "Enter 10 numbers:\n";
for (index = 0; index < size; index++)
cin >> values [index];
}
void square_array (double values [size])
{
int index;
cout << "\nThe square of the numbers are:\n";
for (index = 0; index < size; index++)
{
values [index] = values [index] * values [index];
cout << "\n\n";
cout << values [index];
}
}
void print_cube_root (double values [size]) //this part is not //worknig
{
int index;
cout << "\n\nThe cube root of the numbers are:\n";
for (index = 0; index < size; index++)
{
values [index] = values [index] * values [index];
cout << "\n\n";
cout << values [index];
}
}
•
•
Join Date: Sep 2009
Posts: 398
Reputation:
Solved Threads: 49
0
#2 25 Days Ago
Before the cubing step values[] = original value squared, so saying cubed = values[]*values[] = originalvalue squared * original value squared = original value to the fourth. Also in your parlance, cube_root is the same as cubed which it is not (e.g., cube root of 27 = 3). So, ultimately I think the way you have it laid out is fairly confusing. You could pass in a second result array to each of the functions and leave your original one intact, depending on your specification....
0
#3 25 Days Ago
Hi.
From what i see,
both the functions are identical. Looks like you duplicated one of the functions, but forgot to change the cube-root function.
From what i see,
C++ Syntax (Toggle Plain Text)
void square_array (double values [size]) { int index; cout << "\nThe square of the numbers are:\n"; for (index = 0; index < size; index++) { values [index] = values [index] * values [index]; cout << "\n\n"; cout << values [index]; } } void print_cube_root (double values [size]) //this part is not //worknig { int index; cout << "\n\nThe cube root of the numbers are:\n"; for (index = 0; index < size; index++) { values [index] = values [index] * values [index]; cout << "\n\n"; cout << values [index]; } }
both the functions are identical. Looks like you duplicated one of the functions, but forgot to change the cube-root function.
PEACE !
•
•
Join Date: Oct 2009
Posts: 32
Reputation:
Solved Threads: 0
•
•
•
•
Before the cubing step values[] = original value squared, so saying cubed = values[]*values[] = originalvalue squared * original value squared = original value to the fourth. Also in your parlance, cube_root is the same as cubed which it is not (e.g., cube root of 27 = 3). So, ultimately I think the way you have it laid out is fairly confusing. You could pass in a second result array to each of the functions and leave your original one intact, depending on your specification....
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const int size = 10;
void square_array (int values [size]);
void read_numbers (int values [size]);
void cube_array (int values [size]);
int main(int argc, char *argv[])
{
const int size = 10;
int values [size];
int i [size];
read_numbers (values);
square_array (values);
cube_array (values);
cout << "\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
void read_numbers (int values [size])
{
int index;
cout << "Enter 10 numbers:\n";
for (index = 0; index < size; index++)
cin >> values [index];
}
void square_array (int values [size])
{
int index;
cout << "\nThe square of your input numbers are:\n";
for (index = 0; index < size; index++)
{
values [index] = values [index] * values [index];
cout << "\n\n";
cout << "Square is ---> " << values [index];
}
}
void cube_array ( int values [size])
{
int i;
cout << "\n\nThe cube root of the numbers are:\n";
for (i = 0; i < size; i++)
{
values [i] = values [i] * values [i];
cout << "\n\n";
cout << values [i];
}
}
•
•
Join Date: Sep 2009
Posts: 398
Reputation:
Solved Threads: 49
0
#7 25 Days Ago
Code didn't seem to change too much as it is essentially the same thing. Remember your arrays are being passed as double * so once your functions are returning to main you have changed value arrays. Put some intermediate couts in there to convince yourself. You may have to store the array you get back from read_numbers() into another array and pass that into your cube function.
•
•
Join Date: Oct 2009
Posts: 32
Reputation:
Solved Threads: 0
•
•
•
•
Code didn't seem to change too much as it is essentially the same thing. Remember your arrays are being passed as double * so once your functions are returning to main you have changed value arrays. Put some intermediate couts in there to convince yourself. You may have to store the array you get back from read_numbers() into another array and pass that into your cube function.
Can you please give me example how to do this.. I am really confuse and dont have much idea about arrays..
•
•
Join Date: Sep 2009
Posts: 398
Reputation:
Solved Threads: 49
0
#10 24 Days Ago
Ok. Do you understand about pointers? When you are passing in an array it's (to the best of my knowledge) converted to a type * at compile time to be passed into a function. When your array is being passed into your function it is being changed within the function, so that when you pass it into the next call in main() it is something different.
So basically change your cubing function to
and in main() work off this pseudocode
If you're having trouble with arrays there are tons (and tons) of writeups on the web about them.
So basically change your cubing function to
values[index] = values[index]*values[index]*values[index]; and in main() work off this pseudocode
C++ Syntax (Toggle Plain Text)
declare values array; declare tempvals (same size) read_numbers (values); copy values to tempvals square_array (values); cube_array (tempvals);
If you're having trouble with arrays there are tons (and tons) of writeups on the web about them.
![]() |
Similar Threads
- Change form input text using Javascript/Dom (JavaScript / DHTML / AJAX)
- Help with coding an application that will calculate the square and square root (Visual Basic 4 / 5 / 6)
- PHP Formula and syntax (PHP)
- calculate squares and cube (Java)
- Default Values of Text Field (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: const char array problem
- Next Thread: Internet Provider Switch Program Help
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





