i seem to have messed up on the out put some how

``````#include<iostream>
#include<cmath>
using namespace std;

char ans[16]= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

int digit2value(char);

char value2digit(int);
int conversion(int, int, int);
int reversing(int p[],int i);
int main(){
int radixa,radixb,number;
cout<<"radix a(10) ";// i am going to changing program
cin>>radixa;     // after i can get it to run decimals first
cout<<endl;
cout<<"radix b(2,8,10,16):";
cin>>radixb;
cout<<endl;
cout<<"number to be converted: ";
cin>>number;
cout<<endl;

cout<<reversing;
cout<<endl;

return 0;
}
int reversing(int p[],int i){
int j;
for (j=0;i-1>=0;i--,j++){
ans[j]=p[i-1];
}

return 0;
}
int conversion(int radixa ,int number ,int radixb){

int p[50],i=0,remainder=0;

if (radixa == 10)
for(i=0;number>0;i++){

if(number == 0)

return 0;

else{

remainder = number%radixb;

number/=radixb;

p[i]=remainder;

return p[i];
}
}
}``````
4
Contributors
16
Replies
17
Views
10 Years
Discussion Span
Last Post by skatamatic
Featured Replies
• 1
vmanes 1,165   10 Years Ago

In the reversing function, you don't want to put the reversed content in ans[], you want to put it into p[]. Reversing an array involves a three way assignment - take the value at one end, store it to a holding place, take the value from the other end and …

The only output I see, other than the prompts for data, is `cout<<reversing;` which is displaying the "address" of the function.

Do you mean to call the conversion function first? Do you have a clear picture of the flow of data? what role does the ans[] array play?

To answer your questions: I do mean to call the conversion function first.

I think so from the lesson the teacher has given.
however int main and void main was told to be only different from the return 0; no idea what else is different.

the ans array is something i thought i could use to get around writing a switch function for a hexadecimal base since it was used in another program.
However i don't really know how to incorporate it i am just trying to do things by trail and error. Then i hit a brick wall and got stuck.

OK, your ans[] array could fill the job of getting the hex digits, when you need them. You should not be modifying this array, as your reversing( ) function is doing. You should make ans[] a constant, that is, unchangeble

``const char ans[16]= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};``

and when you do hex conversion, use the value you get for `remainder` as an index into that array. For that matter, do the same with your decimal conversion - you should have only one array type for your solution, and it need to be of type char to handle the hex.

So, your p[] array should be type char as well. And it must be passed to the conversion function, so its result will be known back at main( ).

Why do you return p at the end of the loop - this will exit the function before you've done the work. Try this:

``````int conversion(int radixa ,int number ,int radixb, char p[])
{
int i=0, remainder=0;

if (radixa == 10)
while( number > 0 )
{
remainder = number % radixb;
number /= radixb;
p[i] = ans[remainder];
i++;
}
return i;
}``````

Note that a for loop wasn't really the best way to express the loop condition, you were testing something not related to the loop counter. The value of the loop counter (not what it points to) will be needed back where the function is called from, so it will display only the part of the array that has current data.

Not try to work this into your program.

i worked the code in to code but i am getting the same error i had before when i had ans as a char

``````1.int reversing(char p[],int i){
2.int j;
3.for (j=0;i-1>=0;i--,j++){
4.		ans[j]=p[i-1];
5.}
6.
7.return ans[j];
8.}``````

the error say: " 'ans': you can not assign to a variable that is const "

i am wondering if it would be better if i just get rid of the reversing fn and then went about it in a for loop in the main body
like this

``````for(;p[i]-1;i--){
cout <<p[i];
}
cout<<endl;``````

not sure if this would work with out th first statement though

In the reversing function, you don't want to put the reversed content in ans[], you want to put it into p[]. Reversing an array involves a three way assignment - take the value at one end, store it to a holding place, take the value from the other end and place it at the first, take the value in the temporary store and put it at the other end.

``````An example
array holds       1 2 3 4 5 6 7
store the 1
copy the 7 to the 1's place
copy the stored value to the 7's place

now array is     7 2 3 4 5 6 1
move in to the second and the next to last positions, array becomes
7 6 3 4 5 2 1``````

Keep closing in toward the middle, stop when you've got there.
Don't keep looping for the full size of the array, just half way - until i & j meet.

This function does not need to return anything. It just does it job, and that's it.

Votes + Comments
he was very helpful and gives good hints and examples to make you think of how to run your code

So run the reversing function like this??...

``````int reversing(char p[],int i){
for (int j=0;i-1>=j;i--,j++){
p[j]=p[i-1];
}

return 0;``````

and there seems to me like i have an undeclared i... so how do i call it from the conversion fn??
or like this

``````int reversing(char p[],int i){
for (int j=0;i>=j;i--,j++){
p[i]=p[i-1];
}

return 0;``````

or is it the case of a nest for loop??
ex

``````for(int j=0;j<=i);J++){
for(;i>=j;i--){
p[i]=
}
}``````

p.s. thank you very much for continuing to help me, vmanes, as I create a head ache for you due to my lack coding experience.

If you are going to use i as a counter, don't pass it your function. Rather, declare i inside your function. Seems to me that you want to pass your function the length of the array that is to to be swapped:

``````void reversing (char p[], int lengthArray)
{
int numSwaps = lengthArray / 2;
for (int i = 0; i < numSwaps; i++)
{
// code to swap p[i] and p[lengthArray - i - 1].
}
}``````

In vmanes' example:

``1 2 3 4 5 6 7``

lengthArray would be 7. numSwaps would be 3 (integer division truncates 3.5 to 3). You would need to swap 1 and 7, then swap 2 and 6, then swap 3 and 5. 4 is already in the correct place, so no swap is needed. Also note, as vmanes mentioned, that this is a void function. Nothing needs to be returned. Also, there is no need for a nested for-loop. If you want to keep your two variable for-loop containing i and j, that's fine too. You still would not have a nested loop:

``````for (int j = 0, i = lengthArray - 1; i > j; j++, i--)
{
// code to swap p[i] and p[j]
}``````

not being able to get the code to run with functions made me decide to just try and do it in the main body and get it to work from there so here is my new code however i still can't get the hexadecimal to to switch 15 into f...
So my question now is there something wrong with my switch?

``````#include<iostream>
#include<cmath>
using namespace std;

//const char ans[16]= {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

int main(){
int radixa,radixb,number;
cout<<"radix a(10) ";
cin>>radixa;
cout<<endl;
cout<<"radix b(2,8,10,16):";
cin>>radixb;
cout<<endl;
cout<<"number to be converted: ";
cin>>number;
cout<<endl;

int i=0, remainder=0;
int p[50];

if (radixa == 10){

while( number >0 ){

remainder = number % radixb;

p[i] = remainder;

i++;

number /= radixb;

if (radixb == 16 && remainder >= 10){
switch (p[i]){
case '10':'a'; break;
case '11':'b'; break;
case '12':'c'; break;
case '13':'d'; break;
case '14':'e'; break;
case '15':'f'; break;
}
}
}
}
for (;i-1>=0;i--){
cout<<p[i-1];
}
cout<<endl;
return 0;
}``````

i know this is the easy way out of doing it but it will make it simple to try and convert into a function if i finish the program first

not being able to get the code to run with functions made me decide to just try and do it in the main body and get it to work from there so here is my new code however i still can't get the hexadecimal to to switch 15 into f...
So my question now is there something wrong with my switch?

``````int i=0, remainder=0;
int p[50];

switch (p[i]){
case '10':'a'; break;
case '11':'b'; break;
case '12':'c'; break;
case '13':'d'; break;
case '14':'e'; break;
case '15':'f'; break;
}``````

You've declared p as an integer array, so the "case" line need to be like this:

``````case 10:   /* no quotes around 10 */
// code
break;``````

Not sure what you are trying to do with:

``'a';``

Are you trying to display 'a'?

``case 10: cout << 'a'; break;``

if i convert 65535(radix 10) it =
15151515 in base 16
but i want it to display ffff because 15151515 is not correct

if i convert 65535(radix 10) it =
15151515 in base 16
but i want it to display ffff because 15151515 is not correct

You're going to have to post your full revised program for people to be able to help you, I think.

``````#include<iostream>
#include<cmath>
using namespace std;

int main(){
int radixa,radixb,number;
cout<<"radix a(10) ";
radixa=10;
//		cin>>radixa;
cout<<endl;
cout<<"radix b(2-16):";
cin>>radixb;
cout<<endl;
cout<<"number to be converted: ";
cin>>number;
cout<<endl;

int i=0, remainder=0;
int p[50];

if (radixa == 10){

while( number >0 ){

remainder = number % radixb;

number /= radixb;

if ((radixb > 10) && (p[i] > 9)){
switch (p[i]){
case 10:'a'; break;
case 11:'b'; break;
case 12:'c'; break;
case 13:'d'; break;
case 14:'e'; break;
case 15:'f'; break;
}
}

p[i] = remainder;

i++;}
}
for (;i-1>=0;i--){
cout<<p[i-1];
}
cout<<endl;
return 0;
}``````

changed a little bit and tried to move stuff around but it seems to completely ignore the switch

``````#include<iostream>
#include<cmath>
using namespace std;

int main(){
int radixa,radixb,number;
cout<<"radix a(10) ";
radixa=10;
//		cin>>radixa;
cout<<endl;
cout<<"radix b(2-16):";
cin>>radixb;
cout<<endl;
cout<<"number to be converted: ";
cin>>number;
cout<<endl;

int i=0, remainder=0;
int p[50];

if (radixa == 10){

while( number >0 ){

remainder = number % radixb;

number /= radixb;

if ((radixb > 10) && (p[i] > 9)){
switch (p[i]){
case 10:'a'; break;
case 11:'b'; break;
case 12:'c'; break;
case 13:'d'; break;
case 14:'e'; break;
case 15:'f'; break;
}
}

p[i] = remainder;

i++;}
}
for (;i-1>=0;i--){
cout<<p[i-1];
}
cout<<endl;
return 0;
}``````

changed a little bit and tried to move stuff around but it seems to completely ignore the switch

You are not telling the program to do anything in your switch statement. See my previous post. If you want to display 'a', you need a cout statement. The statement:

``'a';``

does nothing. You need to tell the computer what you want it to do when p = 10. Put this code between:

``case 10:``

and

``break;``

As for displaying 15 instead of 'f', you have defined p as an integer array, not a character array, so it is not going to display a letter.

Look back at the code I posted 13 hours ago - the loop in lines 6-12 will work for any radixb (up to 16) - no special cases needed. Let your result array be of type char, so it holds all "digits" directly. Output then becomes simple again.

There are several functions relating to hex notation/values built in to C++ and its iostream library. Doing some research into those may be supremely beneficial to you.

example...

int i = 10;
cout << hex << i;

output is 'a' - no need for a conversion function

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.