#pragma once
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class boxer
{
private:
bool gender;
int mHP, tournaments;
string fName, lName;
vector<int> stats;
public:
boxer(bool sex, vector<string> boys, vector<string> girls, vector<string> family);
vector<int>getStats();
void setStats(vector<int> stats);
~boxer();
};
#include "stdafx.h"
#include "boxer.h"
#include <vector>
#include <string>
boxer::boxer(bool sex, vector<string> boys, vector<string> girls, vector<string> family) {
bool gender = sex;
int mHP = 100;
int tournaments = 0;
vector<int> stats = { 6,5,4,3,2,1 };
if (gender) {
string fName = boys[rand() % (boys.size() - 1)];
}
else {
string fName = girls[rand() % (girls.size() - 1)];
}
};
boxer::vector<int> getStats() {
return vector<int> {1, 2, 3, 4, 5,6};
};
boxer::setStats(vector<int> stats) {
}
boxer::~boxer()
{
delete &gender;
delete &mHP;
delete &fName;
delete &lName;
};
#include "stdafx.h"
#include "math.h"
#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <boxer.h>
using namespace std;
int main()
{
srand(time(NULL));
vector<string> femaleNames = { "Tonya","Jane","Jessica","Amy","Susan","Tammy","Erica", "Joselyn","Veronica","Laura","Betsy" };
vector<string> maleNames = { "James","Jason","Michael","Christoper","Eric","Cameron","Ralph","Tony","John","Timothy","Craig" };
vector<string> familyNames = { "Jones","Smith","Malloy","Potter","Miller","Doe","Simmons","Johnson","Conner","Monk","Simpson","Mars" };
boxer::boxer(true, malesNames, femaleNames, familyNames) test;
cin.get();
return 0;
}
boxer::boxer(bool sex, vector<string> boys, vector<string> girls, vector<string> family) {
bool gender = sex;
int mHP = 100;
int tournaments = 0;
vector<int> stats = { 6,5,4,3,2,1 };
if (gender) {
string fName = boys[rand() % (boys.size() - 1)];
}
else {
string fName = girls[rand() % (girls.size() - 1)];
}
};
boxer::vector<int> getStats() {
return vector<int> {1, 2, 3, 4, 5,6};
};
boxer::~boxer()
{
delete &gender;
delete &mHP;
delete &fName;
delete &lName;
};
boxer::boxer(true, malesNames, femaleNames, familyNames) test;
First of all, thanks for responding. I appreciate the help. Now to continue my questioning! in the line "boxer test(true, malesNames, femaleNames, familyNames)" which I replaced via your suggestion, the class name "boxer" is underlined red with the error text "Error: Identifier 'boxer' is undefined". I think it may have to do with the fact that my line up top "#include <boxer.h>" is underlined red with a msg saying "Error: cannot open source file boxer.h". What could be causing this problem? I've checked like a million times, my header file name is boxer.h and there's no typo in the "include" call, so I have no idea where else I may of gone wrong.Code:boxer::boxer(bool sex, vector<string> boys, vector<string> girls, vector<string> family) { bool gender = sex; int mHP = 100; int tournaments = 0; vector<int> stats = { 6,5,4,3,2,1 }; if (gender) { string fName = boys[rand() % (boys.size() - 1)]; } else { string fName = girls[rand() % (girls.size() - 1)]; } };
You're declaring new local variables, not assigning your member variables.
The return type should be specified first. vector<int> boxer::getStats()Code:boxer::vector<int> getStats() { return vector<int> {1, 2, 3, 4, 5,6}; };
Do NOT delete variables allocated on the stack. You should only use the delete keyword if you've allocated memory with the new keyword. And you should only allocate memory with the new keyword if you have a good reason for doing so.Code:boxer::~boxer() { delete &gender; delete &mHP; delete &fName; delete &lName; };
Code:boxer::boxer(true, malesNames, femaleNames, familyNames) test;
Should be boxer test(true, malesNames, femaleNames, familyNames);
boxer is a class, not a namespace.
I can answer that if I remember c syntax correctly:
Because it is not a standard library, but a file you provided, you can omit the <> part, and just use #include "boxer.h"? Or do I remember this wrong?
I think it may have to do with the fact that my line up top "#include <boxer.h>" is underlined red with a msg saying "Error: cannot open source file boxer.h". What could be causing this problem? I've checked like a million times, my header file name is boxer.h and there's no typo in the "include" call, so I have no idea where else I may of gone wrong.
using namespace
About this, I've wanted to avoid using Java to develop some software for Android...
Would you, by any chance, know how hard it is to use C# / F# and Unity for this? I may try this summer...
(Although one of the projects need SQL and Unicode support, and not a lot of fancy graphics, and I'm not sure Unity is a better solution for this than normal Android framework, even if I really don't like working with Java)
Yeah, in general any "using x" in a c++ header ends up being trouble. Unless it's scoped, obviously.Unrelated comment but don't ever usein a header. That propagates down the include chain so that every file that includes that header file (directly or indirectly) gets it too. You might get namespace issues somewhere later down the line and at the very least you've defeated the purpose of namespaces.Code:using namespace
Oh shittt.Well, Microsoft just bought Xamarin
namespace Namespace
open UnityEngine
type Script () =
inherit MonoBehavhiour ()
let log = Debug.Log
member this.Start () =
log "Start"
member this.Update () =
log "Update"
member this.OnDestroy () =
log "Goodbye, world"
let [<SerializeField>] mutable variable = "default value"
[<RequireComponent(typeof<MeshFilter>)>]
type Script () =
inherit MonoBehaviour ()
let mutable always68 = 67
let mutateOnce = always68 <- always68 + 1
let mutable variable = 67
let mutate () = variable <- variable + 1
let load<'T when 'T :> Object> filepath = Resources.Load<'T> filepath
let crashesApp = load<Sprite> "Sprites/Mario"
let sprite = lazy load<Sprite> "Sprites/Mario"
let spriteName () = sprite.Value.name
type Loop () =
inherit MonoBehaviour ()
let mutable state = [player]
let view = EntityViewPool ()
let nextState _ = failwith "Not implemented"
member this.Update () =
view.Update state
state <- nextState state
type IState =
abstract Next : Gamepad -> IState
abstract Location : Location
abstract Sprite : Sprite
[<AbstractClass>]
type StateView<'TState> () =
inherit MonoBehaviour ()
abstract member SetState : 'TState -> unit
member this.Show visible =
this.gameObject.SetActive visible
type ViewPool<'TState, 'TView when 'TView :> StateView<'TState>> (childName, parentName, totalKeys) =
let parent = lazy (GameObject parentName).transform
let keys = [0..totalKeys - 1]
let createView i =
let name = childName + i.ToString ()
let obj = GameObject name
obj.transform.parent <- parent.Value
let view = obj.AddComponent<'TView> ()
i, view
let views =
lazy
List.map createView keys
|> Map.ofList
let disableView _ (view : 'TView) =
view.Show false
let disableViews () =
Map.iter disableView views.Value
let updateView (states : 'TState list) key =
let view = views.Value.[key]
view.Show true
view.SetState states.[key]
member this.SetStates states =
disableViews ()
List.iter (updateView states) [0..states.Length - 1]
[<RequireComponent(typeof<SpriteRenderer>)>]
type SpriteView () =
inherit MonoBehaviour ()
member this.Renderer =
this.gameObject.GetComponent<SpriteRenderer>()
member this.SetSprite sprite =
this.Renderer.sprite <- sprite
[<RequireComponent(typeof<SpriteView>)>]
type EntityView () =
inherit StateView<IState> ()
let locationToVector3 (x, y) =
Vector3 (single x, single y, 0.0f)
member this.SetLocation location =
this.transform.localPosition <- location
member this.SpriteView =
this.GetComponent<SpriteView> ()
override this.SetState state =
state.Location |> locationToVector3 |> this.SetLocation
this.SpriteView.SetSprite state.Sprite
type EntityViewPool () =
inherit ViewPool<IState, EntityView> ("Entity", "Entities", 50)
member this.Update states = this.SetStates states
type Fighter =
| Stand of Frame
| Throw of Frame
| Dead
static member Start = Stand 0 :> IState
interface IState with
member this.Next gamepad =
let stand i =
match i, gamepad with
| _, (true, _) -> Throw 0
| 60, _ -> Stand 0
| i, _ -> Stand (i + 1)
let throw =
function
| 60 -> Stand 0
| i -> Throw (i + 1)
let dead = Dead
match this with
| Stand i -> stand i
| Throw i -> throw i
| Dead -> dead
:> IState
member this.Location = 0.0, 0.0
member this.Sprite =
let load path = Resources.Load<Sprite> ("Sprites/Fighter/" + path)
let stand =
let a = load "StandA"
let b = load "StandB"
let c = load "StandC"
function
| i when i < 15 -> a
| i when i < 30 -> b
| i when i < 45 -> c
| _ -> b
let throw =
let a = load "ThrowA"
let b = load "ThrowB"
let c = load "ThrowC"
let d = load "StandB"
function
| i when i < 10 -> a
| i when i < 20 -> b
| i when i < 45 -> c
| _ -> d
let dead = load "Dead"
match this with
| Stand i -> stand i
| Throw i-> throw i
| Dead -> dead
I'm pretty sure each programmer has its strange idea, and my distaste for Java is on the verge of allergy, probably irrational (I even wish Scala or Clojure wouldn't use the jvm). I'm not that fond of C#, but still better.Well, Microsoft just bought Xamarin so I think eventually you'll be able to use Visual Studio to easily write Android projects in C#. For now it might be easiest to just write some Java, it's not that bad especially if you already write C#.
A huge thanks... Too busy now to enjoy it much, but I've saved the page, and I'm sure it'll be really useful when I find the time to get into it.[...]
System.out.print("\nPlease enter number to search for : ");
us = stdIn.nextInt();
//for each loop to print search results
for (int s : ar1)
{
ar1Index++;
//if statement for printing search results
if(s == us)
System.out.println("Search Value: " + s + " found at location: " + ar1Index + " in the unsorted array");
} //end for
System.out.print("\nPlease enter number to search for : ");
us = stdIn.nextInt();
//for each loop to print search results
for (int s : ar1)
{
ar1Index++;
//if statement for printing search results
if(s == us)
{
System.out.println("Search Value: " + s + " found at location: " + ar1Index + " in the unsorted array");
}
else
{
System.out.println("Search Value: " + us + " was not found");
}
} //end for
Nice output, but the instructor also wants it to return a not found message if you search for something that's not in the arraylist. So if I change the code adding an else.
Code:System.out.print("\nPlease enter number to search for : "); us = stdIn.nextInt(); //for each loop to print search results for (int s : ar1) { ar1Index++; //if statement for printing search results if(s == us) { System.out.println("Search Value: " + s + " found at location: " + ar1Index + " in the unsorted array"); } else { System.out.println("Search Value: " + us + " was not found"); } } //end for
It returns every element it searched.
If I search for something not in the array it just says it wasn't found 10 times.
Nice output, but the instructor also wants it to return a not found message if you search for something that's not in the arraylist. So if I change the code adding an else.
If I search for something not in the array it just says it wasn't found 10 times.
If you ever wonder why people look down on Javascript "programmers" read this:
https://news.ycombinator.com/item?id=11348798
It is saying that because you are outputting the not found message inside the loop. It would be better if you reported not found after the search is done (which means after the for loop has completed).
The original code you had can be kept the same to keep reporting if an element was found and at which index. But you should try out different logic for reporting if something was not found anywhere. Know what I mean?
Instead of printing the "not found" message inside the for loop, you could use a boolean variable and change its value if the element was found or not. And check the variable value outside the for loop, that way you just print it once.
Humorous picture time?
The especially humorous thing is that this is actually the future of the web.
https://en.wikipedia.org/wiki/WebAssembly
I mean, it's not *really* web development "with" assembly. But still.
There's been a lot of talk about WebAssembly lately but I still don't quite get how it's supposed to work. How do you target web-specific structures such as the DOM from languages which have no concept of the DOM?
I'm talking out of my ass since I don't actually know, but presumably the DOM is "just some object", and WebAssembly contains instructions for accessing objects.
There's been a lot of talk about WebAssembly lately but I still don't quite get how it's supposed to work. How do you target web-specific structures such as the DOM from languages which have no concept of the DOM?
Why is it so hard to count bits branch free, ugh
Is it? POPCNT, one assembly instruction. Or just use a lookup table.
Unless you are complaining that you're giving this as an interview question and nobody gets it right.
besides using buildin functions, try making one that can be used on multiple platforms, even the ones that have no SSE instructions. I've been fiddling around trying to build a solution without lookup tables and seander algorithms. bits may feel useless, but they do a performance increase in fingerprint recognition.
Cache invalidation?What's wrong with lookup tables? Not only is it trivially portable, and branchless, it's also the fastest possible architecture independent solution
I assume you know about this, but I will post it anyway:Why is it so hard to count bits branch free, ugh
Cache invalidation?
So guys, I will start a cross platform project using C/C++ in my company. The objective is to create one application to run in multiples POS devices. Each vendor provides an API to communicate with their devices and the plan is to create an abstraction for those APIs. Does anyone have tips(articles, books...) to do it in the best way as possible?
Well, Microsoft just bought Xamarin so I think eventually you'll be able to use Visual Studio to easily write Android projects in C#. For now it might be easiest to just write some Java, it's not that bad especially if you already write C#.
What is the size of your data?Why is it so hard to count bits branch free, ugh
B = A & 0x55
A = A >> 1
A = A & 0x55
B = B + A
A = B & 0x33
B = B >> 2
B = B & 0x33
A = A + B
B = A & 0x0F
A = A >> 4
A = A & 0x0F
B = A + B
Is your problem math-related or implementation-related?Write a function quadratic() which determines the number of solutions of a quadratic equation whose coefficients are passed as parameters.
First time poster in this thread but I was wondering if anyone can help me with this demo test for school I've been struggling on. Coderunner is requiring very specific answers of only a function for a program that works out a quadratic. For example, here is another question I got correct:
Write a function IsLeapYear() to determine whether a given year is a leap year or not. The criterion for a leap year is:
1. If a year is evenly divisible by 400, then it is a leap year.
2. Otherwise, if a year is divisible by 100, then it is not a leap year.
3. The only other leap years are evenly divisible by 4.
bool IsLeapYear(int year){
if (year % 400 == 0) return true;
else if (year % 100 == 0) return false;
else if (year % 4 == 0) return true;
else return false;
}
The above code satisfied the requirements but the next one I'm finding a lot trickier as I'm not sure how to exactly give it what it wants. This is what it says:
Write a function quadratic() which determines the number of solutions of a quadratic equation whose coefficients are passed as parameters.
And this is what it expects as the output:
double a = 6, b = 4, c = -7;
if (quadratic(a,b,c) == 2) cout << "There are two roots";
else if (quadratic(a,b,c) == 1) cout << "There is one root";
else cout << "There is no root";
Sorry if any of this is unclear or messy but I'd appreciate any kind of help.
Is your problem math-related or implementation-related?
Just in case, remember that the number of solutions depends on the value of
b*b - 4*a*c
Positive, there's 2 solutions
Zero, there's 1
Negative, there's none.
It's just a matter of comparing b*b - 4*a*c to zero.
That being said, it's tricky with floats. Due to rounding errors, you can get wrong results if b*b - 4*a*c is close to 0, and there's not many things you can do (there is some, but it's not completely safe, and it can be quite difficult). You probably don't mind...
But I'd still say "One root" if b*b - 4*a*c is just "really close to 0", such as between -1e15 and 1e15.
You're trying to compute the roots. You shouldnt, since you just have to return the *number* of roots.Thanks but I think I managed (with the help of youtube) to essentially do the same thing in a regular program which I'll paste below but I don't know how to convert it into just a function and give coderunner what it wants in a similar fashion to the previous example I showed.
root = sqrt(b * b - 4.0 * a * c);
#include <iostream>
#include <cmath>
using namespace std;
// The function you have to complete:
int quadratic(double a, double b, double c) {
double discr = b*b - 4.0*a*c;
if (something)
return 2;
if (something)
return 1;
return 0;
}
// A "main" for testing it:
int main(int argc, char** argv) {
double a, b, c;
// Ax^2 + Bx +C = 0
cout << "Enter coefficients: a, b and c ";
cin >> a >> b >> c;
if (quadratic(a,b,c) == 2)
cout << "There are two roots";
else if (quadratic(a,b,c) == 1)
cout << "There is one root";
else
cout << "There is no root";
return 0;
}
What is the size of your data?
Works with every assembly, need only two registers, no loop/branch, scales easily for larger data, and it's log2(# bits)
Would that fit the bill?
Edit: if the only reason for avoiding LUT is cache invalidation, the solution above may not be faster... I just suggest it as a "no loop, no branch, no LUT, as-efficient-as-possible" solution.
Even more when LUT[A & 0xF] + LUT[A >> 4] gives you a LUT solution that use a LUT long of 8 or 16 bytes only if you really want to avoid cache misses.
No problem, really...Thanks for being patient with me.
Even a 8 bytes long table? That's harsh...this is why I cannot use tables
You need a shorter solution (in term of number or instructions)? Without lut and tests?Your example is the seander algorithm. I tried it, but I still need to reduced with one less instruction.
Your example is the seander algorithm. I tried it, but I still need to reduced with one less instruction.
popcount_2, maybe, yes... It's a variation of the same algorithm, but it spares 2 instructions for 8 bits values, at the cost of some readability. If it's really a matter of a single instruction, it may fit raphier requirements.
(If the hardware is old, I doubt multiplication is usable)