C++ :: Algorithm Producing Wrong Output (Zero)
Jul 14, 2014
Why my calculate function is producing a zero?I feel like it may have something to do with there being zeros the arrays it takes. The arbitrary cout statements are just for my debugging purposes. Input is formatted as follows:
<int>
<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
<string>,<int>,<int>
and so on
#include <iostream>
#include <string>
#include <sstream>
int** parse_input(int &num_items, int &pouch_size);
int* calculate(int &num_items, int &pouch_size, int *values, int *weights, int &max_value);
[Code] ....
View 1 Replies
ADVERTISEMENT
Nov 14, 2014
Something is wrong with option 1. For some reason it adds 1, 2, 3, 4 ... 5 onto the output and I can't think why. Here is a screenshot.
Also how can I make the output a little neater? I tried to use but it just looks horrible.
I had linked the source code! Why is there no edit button?!
#include<iostream>
using namespace std;
int main(){
int option;
cout << "Welcome to my program. Please select a following option.
[Code] .....
View 3 Replies
View Related
Oct 27, 2013
I am working on a project and decided to try something simple before I start adding items. I am calling a function from main and that function has a file pointer.
Here is my main.cpp
Code: #include <cstdio>
#include <string>
#include <iostream>
#include "main.h"
extern FILE *fp;
using namespace std;
int main(int argc, char *argv[])
[code]....
Here is the function:
Code:
#include <string>
#include <cstdio>
#include <iostream>
#include "main.h"
[Code] ....
My test file consists of several characters and digits. Nothing special and I at this point in time do not have any type of formatting that needs to be adhered to. I am simply wanting to read the file character by character and print it out. When I run the program, I get this symbol:
Code: If I use a printf statement, such as:
Code: printf("%s
", nextChar);
I get a segmentation fault.
My main.h Code:
#ifndef MAIN_H
#define MAIN_H
void scanner(FILE *);
//char getChar(FILE *);
#endif and lastly my scanner.h Code: #ifndef SCANNER_H
#define SCANNER_H
FILE *fp;
#endif
View 6 Replies
View Related
Feb 14, 2012
I have the following function:
Code:
public static void Des(byte[] key, byte[] inBuf, byte[] outBuf) {
if (key.Length != 8 || inBuf.Length != 8 || outBuf.Length != 8) {
throw new Exception("Des: Buffer or key length incorrect.");
}
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
[Code] ....
I do not get the expected output of
Code: { 0x88, 0xBF, 0x0D, 0xB6, 0xD7, 0x0D, 0xEE, 0x56 }
instead I get
Code: { 0xC6, 0x61, 0x8D, 0x63, 0x0B, 0x39, 0x31, 0xB0 } am I doing something wrong?
I've even tried reversing the byte order of the test data, and I don't get the right result. I've tested the same data using C++ with CryptoPP on Linux, and I get the expected result, but I'm porting this code to run on .Net, and this is my main stoppage in the process at the moment.
View 5 Replies
View Related
Mar 2, 2014
I have written a program to calculate delay between nodes using link.
Code:
//sim.cc
#include<stdlib.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <cstdio>
int main(int argc, char* argv[]) {
[Code] ....
then I compiled it as follows
g++ -o sim sim.cc
when i run $./sim 2 5
it gives packet delay is 0.0
where is the error?
View 1 Replies
View Related
Aug 31, 2013
I am trying to code a partition quicksort but the problem here is at the output is in an infinite loop:
The input : 4 3 5 7 2
The expected output : 2 3 4 5 7
But my output is infinite :
2 3 4 5 7
2 3 4 5 7
2 3 4 5 7
.
.
and so on
[Code:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/* Head ends here */
void partition(vector <int> ar) {
[Code] .....
View 5 Replies
View Related
Sep 16, 2012
I am trying to code a merge sort algoritm, using my knowledge of C++. I have made some code but it is not working. It is giving some random results.
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
void print(int *niz) {
[Code] ....
View 1 Replies
View Related
Sep 3, 2014
My program works fine in all areas but adding the average every loop. It gives me a weird -1.#IND as an output, and it's supposed to calculate the total mileage each time I enter new values per trip.
// Automobile Mileage.cpp : Defines the entry point for the console application.
//
//Programmer: Ryan Youngen
[Code].....
View 1 Replies
View Related
May 1, 2013
Code:
// Lab0Inventory.cpp : Starter lab
//Anastasia Glyantseva
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
[Code]....
I can't figure out why my makeString gets called first and the pString its called with contains garbage. I want my allocateMem to get called first, but my program is not going to that. What is wrong with the order of my code?
View 3 Replies
View Related
Jan 17, 2012
I have done some programming or rather have written and implemented some algorithms in code (is there a difference? :-)) but am having some teething problems with a project I am currently working on.
To start with I am simply reading in the data from a .txt file into a vector. This is the code that I currently have:
Code:
#include <iostream>
#include <fstream>
#include <cerrno>
#include <vector>
[Code]....
This compiles fine but currently outputs a different integer everytime I run the program. I can remember having this problem when I started coding before using a different language and if I remember correctly it was quite easy to resolve. I thought it was because I had missed off the 'return 0' but it is something similar I think.
View 3 Replies
View Related
Feb 2, 2014
when you enter a name between 'p' and 's' it should send you to room 2432 but no matter what value you enter it says you can get your tickets here.
Code:
#include <stdio.h>
main()
{
[Code].....
View 2 Replies
View Related
Sep 25, 2014
In my program I am supposed to call isEqualTo with a user defined class type, and print out if my two numbers being compared are equal or not. I had to write two versions of this, one with just a template which works fine, but when I implemented the class, all the sudden my program just spews out true no matter if actually equal or not. Here is what I have so far:
#include "stdafx.h"
#include <iostream>
#include "UserClass.h"
using namespace std;
template<typename T>
bool isEqualTo(T value1, T value2){
if (value1 == value2)
[Code] ....
View 4 Replies
View Related
Nov 15, 2012
Consider the following:
Code:
float foo( float const *bar )
)
{
float temp;
temp = *bar;
...
}
Something I am noticing is that temp right after the assignment to *bar, is not the same value as *bar. This is for a project at work and the code runs on an embedded board with an ARM processor. I've copied the function into a standalone program for both Visual Studio and Code::Blocks and it works correctly there.
View 12 Replies
View Related
Feb 19, 2014
I'm writing a program that shows check fees for different amounts of checks. My other fees are showing up right but the .10 cents is not. It's showing up as .08 cents per check when I run the program.
Oh yeah the + 10 is for a $10 fee
Code:
else if (checks < 20 || checks >= 0) {
fee = .10 * checks + 10;
cout<< "Bank service charge for the month is $ " << setprecision(4) << endl;
}
View 3 Replies
View Related
Feb 11, 2013
For an assignment I have to create a random array of four integers, and then I have to allow someone to input up to ten guesses to guess the array in the correct order. I also need to be able to display whatever was generated by inputting -1. Finally, after every guess I have to tell the inputter how many of the guessed integers are correct and in the correct position, as well as how many integers are correct but not in the correct position.
So far I've been able to get the random array to generate properly, but inputting negative one has no effect, although if I input it four times in a row I get to my 'lose' condition. Also, it only seems to allow the user to input 4 guesses and not 10 before going straight to the 'lose' condition. I need to get these issues sorted out before I can move on to showing how many guesses are right etc....
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time (NULL));
[Code] ....
View 3 Replies
View Related
May 24, 2014
My whole code is working but when the results are sent to my phone only the temperature value is reading correct the light value is all jungled up and moisture and pressure values are reading as 0.
Code:
#include "taskFlyport.h"
#include "grovelib.h"
#include "barometer.h"
#include <string.h>
extern BOOL incomingSMS;
extern int incomingIndexSMS;
extern BYTE incomingMemSMS;
[Code]...
View 4 Replies
View Related
May 26, 2014
I am only receiving the correct temperature value. The other 3 values are reading wrong. My format string isent matching the variable list. What I need to do.
float data_bar = 0.0;
float data = 0.0;
float anVal = 0.0;
float moistureVal = 0.0;
while(1) {
// Get the new value using the get() function
[Code] ....
View 3 Replies
View Related
May 12, 2013
I recently wanted to create a (yet) simple program that simulates a mouse movement.So far I managed to make the program work. It does move the mouse, click when expected but the problem is the location it does click at.Here's my code:
#include <Windows.h>
#include <stdio.h>
int leftclick (DWORD x, DWORD y);
int main(){
[code]......
The problem now is: I want the program (for testing purposes) to click at (1920, 1080) and (100, 100) afterwards. Now it does click within a specific range. When I use GetCursorPos to retreive the cursors position it differs quite a bit from where I expected the click to be.
a second question I have is: When I declare the following flag (in the code above) the program does use relative coordinates even though it shouldn't.
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
Where as it works out well when I add a MOUSEEVENTF_MOVE to it.I couldn't find any solution to this in Microsoft MSDN or any other website.
View 2 Replies
View Related
Jan 20, 2013
i'm making a for loop for a mini game which required the user to enter the input number.Let say if the user accidently entered a character instead of integer the whole program will go haywire so is there anyway to check for the error and prompt the user to input the data again?Here is the simple program...
for(row=0;row<4;row++)
{
printf("Enter Row%d:",row);
for(col=0;col<4;col++)
{
scanf("%d",&num[row][col]);
}
View 1 Replies
View Related
Jan 19, 2015
I have been experimenting with a program that loads WAV file and tries to read 1s and 0s from audio file (binary coded information). So far I got working code, but the problem is that I'm not getting correct data. I do get somewhat similar data. The problem is that I get data that amplifies low amplitudes, so instead some noise and binary data I get lots of noise and hard to recognize binary data. My code is this
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
// An unsigned char can store 1 Bytes (8bits) of data (0-255)
[Code] .....
This is wav file in binary viewer (after header)
0030 0032
00300035003200320035003A0048004A
004C0049004A00510049004300360038
and translated to decimal
48 50
4853505053587274
7673748173675456
and this is output from "wavosaur" program (and this one is correct)
0.001465
0.001526
0.001465
0.001617
0.001526
0.001526
0.001617
0.001770
0.002197
0.002258
0.002319
0.002228
0.002258
0.002472
0.002228
0.002045
0.001648
0.001709
You can see it's not the same. I do get the same data like I do from binary viewer, but I don't understand how they are translated to amplitude value. ? Where is the catch?
So this is not really question about c++, but about wav file structure and reading data.
View 2 Replies
View Related
Jul 11, 2014
I have this function that is supposed to take a float as a parameter and then call the getLine() method to accept the users input. The function basically just checks to see if what the user input was of the same data type, if it is it returns the input value, if not then it keeps looping through taking new input until its correct. The problem is no matter what number you put in the output always returns as 140734799803512.
float InputValidation(){
float num;
string strInput;
while (true){
getline(cin, strInput);
[Code] ....
You also need to include <string> and <sstream>.
View 3 Replies
View Related
Sep 6, 2014
I feel as if I'm so close and I'm just messing up a value somewhere, but I am at a loss as to what I should change. Here is my code:
#include "stdafx.h"
#include <iomanip>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
[Code] ....
View 2 Replies
View Related
Dec 17, 2013
Write your question here.
#include<iostream>
using namespace std;
void getInput(int& numOfDays, double& itemPrice);
[Code].....
So this program is supposed to calculate the price of an item, and return it back as the variable total of type int. but for some reason it gives me a weird output on total. And am i using call by reference correct?
View 1 Replies
View Related
Jun 7, 2014
I have a game that requires the switching of tiles. When I try to switch the blank tile (_) with 5, the 5 switches with the 8 instead of the tile switching with the blank tile. The code behaves the same with top row. I have observed that this only happen in the row and column bound by [0][0]; For example the four top left tiles ([8, 7, 5, are 4]) are behave erratically.
Here is sample code!
//swap if columns are equal and blank is to bottom
else if ((blankCol==tileCol) && (blankRow-tileRow==1)) {
temp = board[blankRow][blankCol];
board[blankRow][blankCol] = board[tileRow][tileCol];
board[tileRow][tileCol] = temp;
return true;
}
I am thinking perhaps there may be a double switch but then again why does it for only the left column and top row.
Attached image(s)
View 2 Replies
View Related
Feb 25, 2014
i'm getting a new error is a wrong return type.
WebAPI.cs
namespace DarkAPP___WForm.Libs {
class WebAPI {
System.Net.WebClient wc = new System.Net.WebClient();
public WebAPI() {
[code].....
View 6 Replies
View Related
Nov 12, 2013
Code:
#include <stdio.h>
int main(){
unsigned char x, y=10;
scanf ("%c", &x);
[Code] ....
If I take out scanf and assign x a value then it works, but if I enter, for example, 10 into scanf it comes up as 234 rather than 100.
View 13 Replies
View Related