C plus plus
I am Practicing c plus plus and have run out of ideas. if you have something that you would like me to code post it here and ill post my script. Please note: if you want an exe please say that in your post.
anyway i will post anything i get so please NO FLAMES. I am really going to make these and will ignore stupid post as all of you should.
This isnt something that i want you to make for me, because i've already made it in C, but here's an idea that would be good for you unless you dont know as much about programming as you claim:
Write a program called baseConverter that takes in 3 arguements: number1, base1, base2
the program should then take number1 (which is a number of base=base1) and convert it into base=base2.
Example: baseConverter 1011.01 2 10 –this should output 11.25
the program should be able to handle bases from 2 to 36 and should be able to handle both integer and fixed point decimal numbers
you should be able to do this, it's not too tough once you come up with your conversion algorithms. wow, that really sounded like a teacher giving out a homework assignment.
yours31f wrote: p = rand() % 10000000 +1;
q = rand() % 10000000 + p;
for (p; p<q;p++){ cout<<i1<<" "; }
you are outputting a variable called 'i1' which is never declared, and it never changes. also, your for loop should be increasing by 2 each time, not one because the program was supposed to display only the even numbers between p and q.
i'm curious, did this actually work when you tested it? or did you just post it without testing?
yours31f wrote: i don't dont't like you yroni7, i have still not figured out how to get it to change i know there is a command but i was trying to program it myself.
you dont like me or you dont like my challenge?
i didnt use a command to convert the bases, there isnt one that works universally, just for converting between decimal, hex, and binary i think. try googling for some algorithms, there's lots of them out there.
by the way, you spelled my name wrong.
well, I will give you a little hint in converting from binary to decimal and back, ok, so we have number … eg…. 1101, it is: 1x8 + 1x4 + 0x2 + 1x1 = 13… why this? Well, look, that thing is 1,2,4,8,16,32,64, etc…
back, ok, we have number 13, what is the biggest from 1,2,4,8,16,32, etc? well, 8, how many times? Just once, logical :-), so you have 1 in binary and 5 in decimal left…, ok, so what next? 4? Yes, you can, so 1 in binary again and 1 is left in decimal… can you use 2? No, there is just 1, so 0 in decimal… last one is 1… so result is 1101.
The same you can use for hex, octals, etc.
i'm in a good mood, so i'll give you one algorithm, but you're on your own for the rest: http://www.hitxp.com/math/arth/131202.htm
edit: by the way, that was on the first page of the first google search i just did, so it's not hard information to find.
finally one that works well.
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <math.h>
using namespace std;
int DecToBase(int,long,char*);
int BaseToDec(char*,int);
int GetIndex(char *,char);
//Symbols used to display a number correctly
//Numbers over base 10 use letters to represent values over and equal to 10
//You should be able to increase the max no. of bases by adding other symbols
//Remember a string needs 1 extra space for null character
char symbols[37] = "0123456789ABCDEFGHIJKLMNoPQRSTUVWXYZ";
const int MAX_BASE = 36; //Highest base allowed (make sure there are enough symbols first!)
//
int main(int argc, char* argv[])
{
char number[256] = "";
cout << "Base conversion functions. Written by Yours31f\n\n";
//DecToBase samples
cout << "Examples...\n\n";
DecToBase(2,10,number);
cout << "10dec to binary (base 2): " << number << endl;
DecToBase(8,25,number);
cout << "25dec to octal (base 8): " << number << endl;
DecToBase(3,49,number);
cout << "240dec to base 36 (hexatridecimal?): " << number << endl;
DecToBase(16,28,number);
cout << "28dec to hexadecimal (base 16): " << number << endl ;
//I don't know why I wasted time working this one out... but 998453 spells LEET in base 36
DecToBase(36,998453,number);
cout << "998453dec to base 36: " << number << endl << endl;
//BaseToDec samples
cout << "4C9hex to decimal: " << BaseToDec("4C9",16) << endl;
cout << "1760octal to decimal: " << BaseToDec("1760",8) << endl;
cout << "1101011101bin to decimal: " << BaseToDec("1101011101",2) << endl << endl;
//User input sample
int iInput = 0,iBase = 2, iSrcBase = 10;
cout << "Try another conversion...\nEnter a number in any base between 2 and 36 (must be integer): ";
cin >> number;
cout << "Enter a base to convert from: ";
cin >> iSrcBase;
cout << "Enter a base between 2 and 36 to convert to: ";
cin >> iBase;
//If the source base is not 10, convert it to 10 first
if(iSrcBase!=10)
iInput = BaseToDec(number,iSrcBase);
else
iInput = atoi(number);
//If source base is not 10, and the destination base is, it is not nessecery to run DecToBase as the number
//Has already been found in base 10
if(iSrcBase!=10&&iBase==10)
{
cout << number << " in base " << iSrcBase << " = " << iInput << " in base " << iBase << endl << endl;
}
else
{
if(DecToBase(iBase,iInput,number))
cout << iInput << " in base " << iSrcBase << " = " << number << " in base " << iBase << endl << endl;
else
cout << "Error: Base was out of bounds... (must be between 2 and 36)\n\n";
}
return 0;
}
int DecToBase(int base, long iDec, char* szString)
{
//Check base is between 2 and 36
if(base<2||base>MAX_BASE)
return 0; //Failed
//If input is 0, output is 0
if(iDec==0){
strcpy(szString,"0");
return 1;
}
int count = 0;
char chResult[256] = "";
char* pChResult = chResult;
while(iDec > 0 && count++<256)
{
*pChResult = symbols[iDec % base];
pChResult++;
iDec = iDec/base; //iDec = itself divided by base
}
strcpy(chResult,_strrev(chResult));
strcpy(szString,chResult);
return 1;
}
int BaseToDec(char* number, int base)
{
if(base<2||base>MAX_BASE)
return 0; //Failed
int NumLength = strlen(number);
int PlaceValue, total = 0;
//Work out the place value of the first digit (base^length-1)
PlaceValue = int pow(base,NumLength-1);
//For each digit, multiply by its place value and add to total
for(int i=0;i<NumLength;i++)
{
total += GetIndex(symbols,*number)*PlaceValue;
number++;
PlaceValue /= base; //Next digit's place value (previous/base)
}
return total;
}
int GetIndex(char * pString, char search)
{
int index = 0;
while(*pString != (char)0) //Loop will finish at null character if no match is found
{
if(*pString==search)
break;
pString++;
index++;
}
return index;
}```