Most of the code isn't the sort. You can tell that by just looking at the function names...
Most of the code isn't the sort. You can tell that by just looking at the function names...
I've written bucket sort, to read a list of numbers from a file, sort them, then write the sorted list to a new file. My program runs through the process just as it should, but whenever a bucket is completely sorted, it doesn't save the changes in the bucket. My file consisted of these numbers:
150 221 19 60 42
11 20 94 65
Here is my code (It is a little long, I apologize):
// Bucket Sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
// Functions for file I/O
string getFileName(string);
string getExtension(string);
vector<int> readFile(string);
void saveFile(vector<int>, string);
void printCollection(vector<int>);
// Functions for the sort
int getDigits(int);
vector<int> bucketSort(vector<int>&, int);
int getValueAtDigit(int, int);
int getHighestValue(vector<int>);
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> collection;
string fullFile = "";
string fileName = "";
string extension = "";
string newFileName = "";
cout << "Enter a file name to read data from: ";
cin >> fullFile;
// gets the file naem and extension
fileName = getFileName(fullFile);
extension = getExtension(fullFile);
// creates a new file name for the sorted collection
newFileName = fileName + "-sorted" + extension;
// read the file, put it's contents in a vector
collection = readFile(fullFile);
cout << "Un-Sorted: " << endl << endl;
printCollection(collection);
cout << endl;
collection = bucketSort(collection, getDigits(getHighestValue(collection)));
cout << "Sorted: " << endl;
cout << endl;
printCollection(collection);
cout << endl;
//saveFile(collection, newFileName);
system("pause");
return …
It just doesn't move at all on the second one
Here's pretty much the same thing. Just made it look nicer.
// MazeTraversal.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void printArray(char*[12], int);
void mazeTraversal(char*[12], int, int);
bool isValidMove(char*[12], int, int, int, int);
int _tmain(int argc, _TCHAR* argv[])
{
// array of pointers
char *maze[12];
// Sets each element to a pointer based array
for (int i = 0; i < 12; i++)
maze[i] = new char[12];
for (int y = 0; y < 12; y++)
{
for (int x = 0; x < 12; x++)
{
maze[y][x] = ' ';
}
}
for (int col = 0; col < 12; col++)
{
for (int row = 0; row < 12; row++)
{
(*(maze))[col] = '#';
(*(maze + 11))[col] = '#';
(*(maze + row))[0] = '#';
if (row != 4)
(*(maze + row))[11] = '#';
}
}
(*(maze + 3))[1] = '#';
(*(maze + 3))[2] = '#';
(*(maze + 2))[2] = '#';
(*(maze + 1))[4] = '#';
(*(maze + 2))[4] = '#';
(*(maze + 3))[4] = '#';
(*(maze + 5))[1] = '#';
(*(maze + 5))[2] = '#';
(*(maze + 5))[3] = '#';
(*(maze + 6))[3] = '#';
(*(maze + 7))[3] = '#';
(*(maze + 7))[1] = '#';
(*(maze + 9))[1] = '#';
(*(maze + 9))[2] = '#';
(*(maze + 9))[3] = '#';
(*(maze + 9))[4] = '#';
(*(maze + 9))[5] = '#';
(*(maze + 7))[5] = '#';
(*(maze + 6))[5] = '#';
(*(maze + 5))[5] = '#';
(*(maze + …
You could also use a little javascript
<?php
$tbl_name = "blah";
$sort = mysql_real_escape_string($_REQUEST['sort']);
$data = mysql_query("SELECT * FROM $tbl_name WHERE somecolumn='$sort'");
?>
<html>
<head>
<script type="text/javascript">
</head>
<body>
<form>
<select id="sort">
<option value="..">..</option>
</select>
<input type="button" onClick="redirect();" />
</form>
<script type="text/javascript">
function redirect()
{
var select = document.getElementById("sort").value;
if (select != "")
window.location = "somewebsite.com?sort=" + select;
}
</script>
</body>
</html>
The point of the assignment was also to use pointers, so that's why they're there
I'm writing maze traversal for a school assignment, and I've got an issue. The X will move through most of the maze, but then it will get stuck at a position and keep cycling through. This is probably because I'm not checking properly to see if it hasn't been to a spot already. Here's my code:
/*
Name: Ryan Frappier
Date:
Program: Recursively moves an X
through a maze
Signature:____________________
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
void printArray(char*[12], int);
void mazeTraverse(char*[12], int, int, int, int);
int _tmain(int argc, _TCHAR* argv[])
{
// array of pointers
char *maze[12];
// Sets each element to a pointer based array
for (int i = 0; i < 12; i++)
maze[i] = new char[12];
for (int y = 0; y < 12; y++)
{
for (int x = 0; x < 12; x++)
{
maze[y][x] = ' ';
}
}
for (int col = 0; col < 12; col++)
{
for (int row = 0; row < 12; row++)
{
(*(maze))[col] = '#';
(*(maze + 11))[col] = '#';
(*(maze + row))[0] = '#';
(*(maze + row))[11] = '#';
}
}
(*(maze) + 4)[11] = ' ';
(*(maze + 3))[1] = '#';
(*(maze + 3))[2] = '#';
(*(maze + 2))[2] = '#';
(*(maze + 1))[4] = '#';
(*(maze + 2))[4] = '#';
(*(maze + 3))[4] = '#';
(*(maze + 5))[1] = '#';
(*(maze + 5))[2] = '#';
(*(maze + 5))[3] = '#';
(*(maze + 6))[3] = '#';
(*(maze + 7))[3] = '#';
(*(maze …
Look on YouTube for a tutorial on 'Pageination' by PhpAcademy. It will provide you with the information that you need. I just learned how to do it recently. I've always wanted to do it, but I couldn't figure out what the darn technique was called to search for it :p
Try uninstalling Xampp and installing Wamp server. I had the same issue, and I couldn't get xampp to work at all. So I installed Wamp, and it still wasn't working. I had to go into the httpd.config file and change the listen port from 80 to 8080. Then when you want to go to your sever you have to type either 127.0.0.1:8080 or localhost:8080. Hope that helped some!
I believe that one of the pages of the site that I'm creating is shifted slihtly to the left. I may be going crazy, but I'm pretty sure that's what's happening. The page that I believe is shifted is: http://armyants.us/members.php?steam=All If you guys could help me out here, then that's be pretty awesome. Thank you for your time!
I fixed it by putting a wrapper around the divs. thanks for the help guys!
I have a page that loads dynamic content (comments, to be specific). The comments are floated to the left, and the "Add a comment" div is floated right. For some reason, the add a comment div is being pushed down by the comment divs. The link to the page is: http://armyants.us/comments.php
Hello, I am trying to fill an array with 20 unique random values between 1 and 60. I know what I need to do to get this to work, but I'm not sure how to do it. Here's what I've got:
void fillArray( int arr[], int size)
{
int arr2[20];
bool found[20] = {false};
for (int i = 0; i < size; i++)
{
arr[i] = rand() % 60 + 1;
arr2[i] = arr[i];
for (int j = 0; j < size; j++) // check for duplicates in array
{
if (arr[i] == arr[j])
{
found[i] = true;
arr[i] = rand() % 60 + 1;
}
}
}
}
My program checks an array for duplicates, but doesn't check the duplicates to see if they themselves are duplicates. How might I go about doing that? Thank you for your time!
Thanks! My max file size is 2 MB. However, an image that I tried to upload is less than 2 MB of memory
Hi, I am recieving these two errors when attempting to upload an image file to my server:
Warning: move_uploaded_file(images/) [function.move-uploaded-file]: failed to open stream: Is a directory in }[I removed the path, this is not part of the error] on line 13
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php2bBihw' to 'images/' in [i removed the path again for security reasons] on line 13
I'm using 000webhost as a free user, and I checked the permissions on the forder, and they are at the correct settings. So I'm not exactly sure what's wrong. If you need my php script that I used to upload, or the path to the file (which I doubt will be necessary), then I can provide them. Thank you for you assistance and time!
Hello, my website looks perfectly fine on my iPad (which I created it on) however some of the divs such as for comments overlap other divs. I'm not sure why that is. Also, once you login, the header of the website gets cut off. I think this is due to some sort of CSS error, but I'm not sure. The link to my website is: rfrapp.netau.net/mywebsite/index.php. You can use "admin" and "password" to login in the user login section. Thank you in advance for your assistance. I can provide code for any pages on request.
Hello, I am trying to upload music files to a database for use on my website. However, the file doesn't seem to actually be going into the folder. I get the name of the file in phpmyadmin, but I don't see the file anywhere. Here's my script (I do have a directory in the database named music, so that doesn't seem to be the problem):
<?php
include "db_connect.php";
$tbl_name = "music";
$title = $_REQUEST['title'];
$type = $_REQUEST['type'];
$musicuploaddir = "music/";
$song = $musicuploaddir . basename($_FILES['song']['name']);
mysql_query("INSERT INTO $tbl_name (id, song, title, type) VALUES ('', '$song', '$title', '$type')");
$entrynumber = mysql_insert_id();
if ($song != "music/" && move_uploaded_file($_FILES['song']['tmp_name'], $musicuploaddir))
{
$songnewname = $musicuploaddir . $entrynumber . ".mp3";
rename($song, $songnewname);
mysql_query("UPDATE $tbl_name SET song='$songnewname' WHERE id=$entrynumber") or die( mysql_error() );
header("Location: music.php?song=$songnewname");
}
?>
What's the solution?
Hello, I'm creating a game using SDL, and what I'd like to happen is: if the character jumps on a button, then it'll disappear. However, I cannot find a way of accomplishing this, as all the SDL functions I know of start at runtime and cannot be changed. If I'm wrong, would someone please enlighten me? Is there a way that I can get this accomplished? Thank you for your time!
I pointed you to the bug. It is at line 44. What is it supposed to do? What does it do?
It represents the middle of the array. It's used to separate the array into two parts so it can be searched easier.
Explain line 44.
I'm gonna be honest with you. My teacher gave the class part of this code, and said fix it. That's just what he had already written :p
Hello, I need to demonstrate the binary search using recursion, and I've run into a little problem. In my example, if "key" was 1, 2, 3, or 6, then it would return true. Anything else would give me an error. Why is that? Here's my code:
// binary search.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
bool binarySearch(int[], int, int, int);
int _tmain(int argc, _TCHAR* argv[])
{
int col[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int input;
int x = 0;
do
{
cout << "The collection: " << endl;
for (int i = 0; i < 10; i++)
{
cout << col[i] << "\t";
if (i == 4)
cout << endl;
}
cout << endl << endl;
cout << "Enter a number to see if it is in the collection: " ;
cin >> input;
binarySearch(col, 0, 10, input);
if (binarySearch(col, 0, 10, input) == true)
cout << "This number is in the collection!" << endl;
else
cout << "This number is not in the collection!" << endl;
x++;
system("pause");
system("cls");
} while (x < 10);
system("pause");
return 0;
}
bool binarySearch(int col[], int start, int end, int key)
{
int mid = (end - start) / (2 + start);
if (col[mid] == key)
return true;
else if (col[mid] < key)
{
if (mid > end)
return false;
else
return binarySearch(col, mid + 1, end, key);
}
else
{
return binarySearch(col, start, mid, key);
}
}
For my final project in school, I want to create a platforming game, and I want to create UML for the game before I actually do any coding. However, I'm not sure where to start, as I haven't done much with UML. Any suggestions to help get me started?
I'm trying to finish this assignment for class, and It seems as though I'm not calculating the neighbors correctly. It seems correct to me, but I'm not sure. Also, the rules for the game are not being applied to the new generation. Any help would be appreciated. Thank you for your time. Here's my code:
// Life.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
void generation(char[][20], int);
void neighbors(char[][20], int);
void display(char[][20], char[][20], int, int);
int _tmain(int argc, _TCHAR* argv[])
{
int x = 0;
char board[70][20];
char s[2][2];
srand(time(NULL));
generation(board, 70);
system("pause");
return 0;
}
void generation (char board[][20], int size)
{
int x = 0;
int number;
for (int x = 0; x < 20; x++) //initial configuration
{
for (int y = 0; y < 70; y++)
{
board[y][x] = ' ';
board[34][10] = 'X';
board[35][10] = 'X';
board[36][10] = 'X';
board[19][6] = 'X';
board[20][6] = 'X';
board[21][6] = 'X';
board[49][6] = 'X';
board[50][6] = 'X';
board[51][6] = 'X';
}
}
neighbors(board, 70);
}
void neighbors(char board[][20], int size)
{
char newBoard[70][20];
int nCount = 0;
int z = 0;
/*
Rules:
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with …
There is a number of problems with the code. I don't know where to start.
To address your immediate question (my code will always make the new generation blank), check with the debugger (or just print out) the value of nCount. You will be surprised.
Second, your display() function is really funny. It displays the original board (once), and then goes on to display the same newBoard (which never changes anymore) for 50 times.
Third, your neighbour counting is plain wrong. In the game of life all 8 neigbours count, along with the cell itself.
Finally, when counting neighbours you have your indices backward (board defined to have first index in 0..69, and second index in 0..19), and you do step outside the board.
That's all I see with the naked eye. Maybe, there's more, but that's enough to keep you busy for now.
1. I realize what you mean about nCount. I've fixed that now
2. My display function is supposed to display the initial configuration once, and then the new configuration afterwards. The reason that it is displaying thee same thing is because the changes haven't been made to the new array. I'm unsure of how to fix this.
3. I've now counted all 8 neighbors; I'll post my code
void neighbors(char board[][20], int size)
{
char newBoard[70][20];
unsigned int nCount;
int z = 0;
/*
Rules:
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any …
$db_host = //host; $db_user = //username; $db_pass = //password; $db_database = //database;
just to double check you got values instead of //host, and if so you have closed all your apostrophes and speech marks?
Yeah, there are actuall pieces of information in place of the comments, and I've made sure that everything is closed
what's the error say?
Here's what it says:
Parse error: syntax error, unexpected T_STRING in /home/a1142446/public_html/php/login_process.php on line 10
This is the beginnings of a login script that I've been working on, and I can't seem to figure out why I have this error. The error is said to be on line 10. Any help would be appreciated. I've changed the info in the variables used for connection just for security reasons. Thanks!
<?php
session_start();
/* Database config */
$db_host = //host;
$db_user = //username;
$db_pass = //password;
$db_database = //database;
$link = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_database, $link);
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$data = mysql_query("SELECT * FROM $tbl_name WHERE username=$msuser AND password=$mspass");
$count = mysql_num_rows($data);
$username = $_SESSION['username'];
$password = $_SESSION['password'];
?>
I'm trying to finish this assignment for class, but I've got a problem.It seems as though my code will always make the new generation blank, and I'm not sure why. If you could read my code, then I'd greatly appreciate it. Thanks!
// Life.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
void generation(char[][20], int);
void neighbors(char[][20], int);
void display(char[][20], char[][20], int, int);
int _tmain(int argc, _TCHAR* argv[])
{
int x = 0;
char board[70][20];
char s[2][2];
srand(time(NULL));
cout << endl;
generation(board, 70);
system("pause");
return 0;
}
void generation (char board[][20], int size)
{
int x = 0;
int number;
for (int x = 0; x < 20; x++) //initial configuration
{
for (int y = 0; y < 70; y++)
{
board[y][x] = ' ';
board[10][34] = 'X';
board[10][35] = 'X';
board[10][36] = 'X';
}
}
neighbors(board, 70);
}
void neighbors(char board[][20], int size)
{
char newBoard[70][20];
int nCount;
int z = 1;
/*
Rules:
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
for (int x = 0; x < 20; x++)
{
for (int y = 0; y < …
My assignment is to create the Game of Life. I know that it is very easy to find out how to do this online, but I don't want the answers fed to me. Here's what I have so far:
// Life.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
void generation(char[][20], int);
void neighbors(char[][20], int);
void display(char[][20], int);
int _tmain(int argc, _TCHAR* argv[])
{
int x = 0;
char board[70][20];
srand(time(NULL));
for (int x = 0; x < 70; x++)
{
for (int y = 0; y < 20; y++)
{
board[x][y] = 'X';
//cout << board[x][y];
}
}
cout << endl;
generation(board, 70);
system("pause");
return 0;
}
void generation (char board[][20], int size)
{
char symbol = 'X';
int x = 0;
int number;
for (int x = 0; x < 70; x++) //uses rand to populate the board
{
for (int y = 0; y < 20; y++)
{
number = rand() % 100 + 1;
if (number > 50)
board[x][y] = symbol;
else
board[x][y] = ' ';
cout << board[x][y];
}
}
do
{
neighbors(board, 70);
x++;
system("pause");
system("cls");
} while(x < 50);
//display(board, 70);
//system("pause");
//system("cls");
}
void neighbors(char board[][20], int size)
{
char newBoard[70][20];
for (int x = 0; x < 70; x++)
for (int y = 0; y < 20; y++)
{
newBoard[x][y] = board[x][y];
//this would fullfill the requirement for overpopulation
if (newBoard[x][y] == 'X')
if (board[x][y - 1] == 'X' …
Well, I guess you have already handed in your homework since it is the end of Jan 31 so I guess I can spill some more beans. It seems as though your algorithm is more complex than necessary. But then maybe I don't know exactly what you are trying to accomplish as far as formatting, etc,. In any case, here is a method of converting a double decimal value (a whole positive number) into a hex string. Maybe you can find something helpful in it:
#include<iostream> using std::cout; using std::cin; using std::endl; int main() { cout << "\n\nInput number: "; double answer; cin >> answer; double divisor = 1; for(; divisor <= answer; divisor *= 16); //loop generates divisor divisor /= 16; //adjusts divisor to be < answer int hexDigit = 0; //holds a hex digit do { hexDigit = static_cast<int>(answer / divisor); //generates a hex digit if(hexDigit > 9) //if digit > 9, cout << static_cast<char>(hexDigit + 55); //then print A-F, else //else just print digit. cout << hexDigit; answer -= (hexDigit * divisor); //answer now contains remainder divisor /= 16; //adjust divisor for next loop }while(divisor >= 1); //end loop when divsor is < 1 cout << endl << endl << endl; return 0; }
Hello, I did turn it in, and unfortunately it was incomplete, but I'm curious as to mine isn't working correctly. Here's my code:
void backToHex( int result[], int result2[], int size)
{
char hex[16] = {'0', '1', '2', '3', '4', '5', '6', …
Then get it out of your head and on your desk with pencil and paper. The mind is a very bad analog for the computer.
When programming, the last thing you do is type the code in. Before that you need to sit at a desk and write down your steps, eventually convert that to code, and execute that code line by line on paper to iron out the details and problems you forgot about.
Once it looks good on paper, that's the time to type the program in.
You'll thank me later... :icon_wink:
I got the whole thing written today, however, there's one thing that I'm not sure of. How exactly can I pass a variable from one function to main? I don't have my code as an example at the moment, but I'll have that later if you need it.
There are two loops in the bubble sort. With one loop, essentially, you're only doing one pass, sorting the highest value to the top, but neglecting the others.
This may prove useful: http://www.codecodex.com/wiki/Bubble_sort
Also, you may want to change that i==5 to i==4. When i equals 5, you'll be starting your second line, but you're putting in the line break after you've printed arr[5]. Since you 10-element array goes from 0...9, you'll be printing something like
0 1 2 3 4 5
6 7 8 9Hope this was helpful.
Happy coding!
Thanks! I see what you mean about changing i, and I haven't gotten a chance to look at the link fully yet, but it seems to have all the information that i'll need!
I'm writing a program that is supposed to search an array between the values of -1000 and 1000, as well as sort it using the 3 different sorts (bubble, insertion, selection). I have the searching part done, but my bubble sort isn't working right. It seems right in my head, so I'm not sure what's wrong. It doesn't seem to be outputting the sorted array either.
// sorts.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void search(int, int[], int, bool);
void selection(int[], int);
void bubble(int[], int);
void insertion(int[], int);
void reset(int[], int);
int _tmain(int argc, _TCHAR* argv[])
{
int choice;
int number;
bool cont = true;
int arr[10]; //= {49, 100, 2, 503, 203, -504, 23, 75, 900, -4};
const int min = -1000;
const int max = 1000;
do
{
cout << "The collection: " << endl;
for (int i = 0; i < 10; i++)
{
arr[i] = rand() % (max - min) + min;
if (i == 5)
cout << endl;
cout << arr[i] << "\t";
}
cout << endl;
cout << "(1) Search the array" << endl;
cout << "(2) Sort with the Selection Sort" << endl;
cout << "(3) Sort with the Bubble Sort" << endl;
cout << "(4) Sort with the Insertion Sort" << endl;
cout << "(5) Reset the collection" << endl;
cout << "(6) Quit" << endl;
cin >> choice;
switch (choice)
{
case 1:
//search the collection
cout << "Which …
Sometimes it helps to see concrete examples:
int r1 = rand() ; //random number from [0 , RAND_MAX] int r2 = rand() % 100; // random number from [0,100); Note doesn't include 100 int r3 = rand() % 100 + 1; //random number from [1,100]; Note includes 100 const int max = 100; const int min = -100; int r4 = rand() % (max - min) + min; // random number between [min,max)
When you want to look up a function, you can see examples here. Bookmark it if you need to. Its a good reference
How do i mark a thread solved? I'm relatively new here :p
Sometimes it helps to see concrete examples:
int r1 = rand() ; //random number from [0 , RAND_MAX] int r2 = rand() % 100; // random number from [0,100); Note doesn't include 100 int r3 = rand() % 100 + 1; //random number from [1,100]; Note includes 100 const int max = 100; const int min = -100; int r4 = rand() % (max - min) + min; // random number between [min,max)
When you want to look up a function, you can see examples here. Bookmark it if you need to. Its a good reference
Thanks! The example worked well! I figured it out now.
For part of a programming that I'm writing, I want to generate random numbers between the range of -1000 and 1000. My teacher never spent much time on the rand function, and I'm not quite sure how to specify a range like that. Any help would be greatly appreciated. Thanks!
while(input.length()<10)input="0"+input;
same for
input2
When add this into the for loop in my "convertToDec" function, I get a runtime failure unless there are 0's before the number.
My bad.
Turns out reversing the loops wasn't such a good idea. It doesn't fix anything, just brakes some more.
The point here is, that if you convert strings to numbers for computation, they should be of the same length. For the program to work properly, you have to add '0' at the beginning of both strings until their length is 10.
I see what you mean. I'm not sure how to accommodate for that though. What would you suggest I do? Please excuse my ignorance, I probably sound very helpless, but I am trying.
Ok, now I get it.
It was in this function.
Your problem is that you output the numbers reversed. Just change the loops in lines 221 and 224 forfor (int i = 9; i >= 0; i--)
should work.
And really, DO learn debugging - with it I discovered your problem in matter of minutes. Plus, debugging in Visual Studio is IMO pretty intuitive.
Well, that fixes the problem with how it outputs, but it doesn't output the right answer. Try entering in AB for the first input, and 1 for the second. It gives you BB. Which means that it added the 1 to the A, rather than the B like it's suppowed to. How do I fix this?
Sorry, for line ten it should be
answer[i] += answer[i] + result[i] + result2[i];
forgot to add the plus after copying.Didn't your teachers tell you how to debug? Argh...
The place where your mistake lies - that I can't check with just this much code. That is why I asked you to check it yourself, but you say you don't know how.
Either enter your IDE's name in google adding "debugging tutorial" or post the whole code.
Here's my full code:
// hex addition.cpp : Defines the entry point for the console application.
//
/*
useful character functions:
char toupper( char ) -> myChar = toupper( myChar );
bool isdigit( char ) -> true if a digit
bool isalnum( char ) -> true if alphanumeric
char tolower( char )
bool isalpha( char ) -> true if alphabetic
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void convertToDec(string, string);
void reassign(int[], int[], int);
void backToHex(int[], int[], int);
int _tmain(int argc, _TCHAR* argv[])
{
//assign values to last six hex characters
//convert both inputs to dec
//add two dec numbers
//convert back go hex for answer
string input;
string input2;
int digit = 0;
bool decision;
char choice;
cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
do
{
decision = true;
cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
cin >> input;
cout << "Now enter another hexadecimal number to add to the first: ";
cin >> …
One error I see is in line 10. it should be
answer[i] = answer[i] + result[i] + result2[i];
otherwise the carry-over wont work.This function seems OK, the problem is most likely with the format of the results - you seem to keep them starting from the highest exponent and omit the leading zeroes.
Try debugging it. Set a breakpoint at the beginning of this function and see what the results look like.And two tips:
Use pass by reference or pass by pointer here - right now you just unnecessarily copy those arrays.
Makechar hex[16]
const - this will help the compiler optimize this function.
I'm new to c++, and I don't know how to implement those things that you're talking about. I'm very stressed, this program is due tomorrow. I didn't slack off, I've been trying to get this for a while
I'm writing a program that adds two hexadecimal numbers. There seems to be a problem now. If I enter AB for the first input, and 1 for the second, then it gives me BB as the result. Which, obviously, is wrong. The answer should be AC. The problem is in this function, and I can't figure out how to fix it. Here's my code:
//converts answer in decimal to hex
void backToHex( int result[], int result2[], int size)
{
char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int answer[10] = {0};
for (int i = 0; i < 10; i++)
{
answer[i] = answer[i] + result[i] + result2[i];
if (answer[i] > 15)
{
answer[i + 1] = answer[i] / 16;
answer[i] %= 16;
}
}
if (answer[9] > 15)
cout << "----------Addition Overflow----------" << endl;
else
for (int i = 0; i < 10; i++)
cout << hex[answer[i]];
cout << endl << answer[0] << answer[1] << endl;
}
I realized that I've been doing the assignment wrong. There was no need to get a real number. I just need to store the values in arrays. Here's what I've done: however, it seems to add the two arrays incorrectly
// hex addition.cpp : Defines the entry point for the console application.
//
/*
useful character functions:
char toupper( char ) -> myChar = toupper( myChar );
bool isdigit( char ) -> true if a digit
bool isalnum( char ) -> true if alphanumeric
char tolower( char )
bool isalpha( char ) -> true if alphabetic
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void convertToDec(string, string);
void reassign(int[], int[], int);
void backToHex(int[], int[], int);
int _tmain(int argc, _TCHAR* argv[])
{
//assign values to last six hex characters
//convert both inputs to dec
//add two dec numbers
//convert back go hex for answer
string input;
string input2;
int digit = 0;
bool decision;
char choice;
cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
do
{
decision = true;
cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
cin >> input;
cout << "Now enter another hexadecimal number to add to the first: ";
cin >> input2;
cout << endl;
if (input.length() > 10)
{
cout << "Invalid input" << endl;
system("pause");
return 0;
}
if (input2.length() > 10)
{
cout << "Invalid input" << endl;
system("pause");
return 0;
}
convertToDec(input, input2);
cout << "Would you like to …
I need to add two hexadecimal numbers together. I've converted the two to decimal, but I can't figure out how to convert to hexadecimal. This needs to work for up to ten digits. I can't use standard i.o. or any other function. I need to do it manually. Here's my code, only the bottom lines really matter, but i included the rest for reference:
// hex addition.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void convertToDec(string, string);
void add(double, double);
void backToHex(double);
double mod(double, double);
int _tmain(int argc, _TCHAR* argv[])
{
//assign values to last six hex characters
//convert both inputs to dec
//add two dec numbers
//convert back go hex for answer
string input;
string input2;
int digit = 0;
bool decision;
char choice;
cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
do
{
decision = true;
cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
cin >> input;
cout << "Now enter another hexadecimal number to add to the first: ";
cin >> input2;
cout << endl;
if (input.length() > 10)
{
cout << "Invalid input" << endl;
system("pause");
return 0;
}
convertToDec(input, input2);
cout << "Would you like to continue? (y/n)";
cin >> choice;
if (choice == 'n' || choice == 'N')
{
decision = false;
system("pause");
return 0;
}
if (choice == 'y' || choice == 'Y')
{
decision = true;
system("pause");
system("cls"); …
That's a massive algorithm. Okay, well I don't know if you have to generate an algorithm for it or not, but if you don't I believe the ToString method still works in C++.
myDecimalValue->ToString("x");
I'm not 100% sure on it, but that's what we use in C# because it's simpler; I have an algorithm in C# but I don't have enough C++ knowledge to convert it over completely.
I'm not sure if I can use that. My teacher wants us to do all of this manually
I have been assigned a project to add two hexadecimal numbers of up to ten digits. I've taken the two inputs, and converted them to decimal. But I can't figure out how to convert the decimal number back to hexadecimal. I really need some help with this because the program is due Tuesday (Jan 31). I've been staring at a computer screen trying things all weekend, and I can't figure it out. Here's my code: it's long, so you don't have to read it if you don't want to. I tried to write a function to mimic modulus as well, and that didn't work out. Thanks for all of your help!
// hex addition.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void convertToDec(string, string);
void add(double, double);
void backToHex(double);
double mod(double, double);
int _tmain(int argc, _TCHAR* argv[])
{
//assign values to last six hex characters
//convert both inputs to dec
//add two dec numbers
//convert back go hex for answer
string input;
string input2;
int digit = 0;
bool decision;
char choice;
cout << "Welcome to the Hexadecimal Addition Calculator!" << endl;
do
{
decision = true;
cout << "Enter two hexadecimal numbers (no more than ten digits): " ;
cin >> input;
cout << "Now enter another hexadecimal number to add to the first: ";
cin >> input2;
cout << endl;
if (input.length() > 10)
{
cout << "Invalid input" << endl; …
Hi again,
If you have a .htaccess file already it should be in the same folder as your index.php page. If you don't already have one you can create your own. Use notepad or another simple text editor, type in the line I gave you in my last post, then save the file as
.htaccess (notice the dot)
NOT
.htaccess.txt
It's a bit of an odd way to name a file but that's the how it is :)
That worked perfectly! Thanks again!
If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.OR in pseudocode, maybe somehting like this:
x, y, remainder remainder = y while x less than or equal to remainder remainder = remainder - x
If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.OR in pseudocode, maybe somehting like this:
x, y, remainder remainder = y while x less than or equal to remainder remainder = remainder - x
Here's what I've done:
void backToHex( double answer )
{
int a = answer;
char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int remainder[11] = {0};
if (remainder[11] != 0)
{
cout << "----------Addition Overflow----------" << endl;
}
remainder[0] = mod(answer, 16.0);
cout << remainder[0];
}
double mod(double answer, double y)
{
double remainder = y;
while (answer <= remainder)
remainder = remainder …
If you feel you must continue with your approach, and you really want to try to use modulus on a type double, then you could write your own function to mimic the modulus operator for ints.
take 2 variables x and y;
if x < y what is the remainder if you remove as many x's from y as you can.OR in pseudocode, maybe somehting like this:
x, y, remainder remainder = y while x less than or equal to remainder remainder = remainder - x
Here's what I've done:
void backToHex( double answer )
{
int a = answer;
char hex[16] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int remainder[11] = {0};
if (remainder[11] != 0)
{
cout << "----------Addition Overflow----------" << endl;
}
remainder[0] = mod(answer, 16.0);
cout << remainder[0];
}
double mod(double answer, double y)
{
double remainder = y;
while (answer <= remainder)
remainder = remainder - answer;
return remainder;
}
However, It always returns 16. Why is that?