C++ :: Incrementing Values In Vector Based On Variable Input

Mar 24, 2014

I'm trying to increment the values in a vector, not the vector size, based on variable input. Basically I have a vector of size 10, and all of its values are initialized at zero. The program counts the frequency of numbers 0-9 in a four digit user input. This is what I have (I want it to work so badly but the compiler says that I'm using a pointer to a function used in arithmetic):

for (int i=0; i < num_slots; ++i) {
++guess_frequency[guess[i]];
}

I just want to know if you can increment values within a vector:

E.g.
change
0 0 0 0 0 0 0 0 0 0
to
1 0 0 0 2 0 0 1 0 0

View 6 Replies


ADVERTISEMENT

C :: Incrementing Number - Variable Count Stays On 1

Mar 3, 2013

I am forking 3 times in a loop like this but the variable "count" does not increment, it stays on '1' and therefore this is an infinite loop, and this simple thing dont make sense to me.

I have checked so that the pointer address is the correct one every loop.

Code:
void increase(int* x) {
*x += 1;
}
main() {
int pid, i, number = 0;

[Code] .....

View 8 Replies View Related

C++ :: Can't Store Different Values Into Vector By User Input

May 4, 2013

I can`t seem to store multiple values into my vector when a user keys in an input. Lets say a user inputs 1,2 as the first set and later keys in 3,4.

My vector gets overridden by 3 and 4. I'm trying to pass these values into a function to store them into a vector and use them in the function.

Below is my code snippet

int reconstructSecret(int x, int y) {
int getX,getY;
getX=x;
getY=y;
vector<int> myXCordVec;
vector<int> myYCordVec;

[Code] .....

View 6 Replies View Related

C++ :: Write A Program That Replaces Values In A Vector With Their Absolute Values

Dec 4, 2013

This is my code: [tag]

Code:
#include <iostream>
#include <string>
#include <cstring>
#include <vector>

using namespace std;

[Code] .....

View 4 Replies View Related

C++ :: Item Search Based On 2 Values For Index?

Apr 4, 2013

I have to be more code specific (I usually try to abstract my code). As you can see I use a lot Qt classes... however they work very similar to standard libraries, so it should be not a problem to look at this question.

class FrameManager {
QList<FRAME> frameData;
public:
int frameID(int grpno, int imgno);
};

[Code] ....

Now I know the example search only the ID and doesn't allow the access of the actual data... this is not the question.

The problem is... I will need to define an AnimationManager class that will collect sequence of frames (identified by the unique couple of value) in animations... so it will need to use a massive search of item from the couple of value (and not ID, unluckly)

I fear if I use an approach similar to the one written in the example (search from the start to the end until the element is found) can be very unefficient.

In the same time the AnimationManager should always know if frame exist or not (if not exist an invisible image will be shown) and if in the meantime changed.

Another problem is that I cannot order the data sequence inside FrameManager (becouse it is expected to mantain the original order, even when it is chaotic).

I tried to take a look around QMap (or std::map) but it is not clear at all how the optimiziation works and how I can use it in my case

I tried also to take a look at the "hash" concept, but for me it is too complex to understand deeply.

So... I am feeling like I am entrapped... I am unable to find a good "exit"...

View 8 Replies View Related

C/C++ :: Sorting A File Based On ASCII Values?

Sep 1, 2014

New file_sorter.c:

#include <stdio.h>
#include <stdlib.h>
#include "sort.h"
#include <string.h>

[Code].....

My issue is now a segmentation fault in my sorting algorithm. In my assignment I was suppose to implement quick and insertion sort for the file, and organised strictly by ASCII values. We were allowed to look up generic algorithms for the sorts, but we have to convert them to comparing char arrays.

I haven't started trying to configure quick sort yet, but I'll supply the generic algorithm as well. keep in mind that the first line in the file contains the number of lines in the file. I'll try not to disappear this time!

sort.c :
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

[Code]....

View 4 Replies View Related

C++ :: How To Control Speed At Which A Loop Loops Based On Outside Variable

Feb 20, 2015

I'm using an animation program. In this program I've simulated a particle system. The particles are flying around at different and varying speeds. I've attached birds to the particles and I want to be able to control each bird's flapping animation based on its velocity; so birds moving faster will be flapping faster.

Initially, the bird's flapping animation is controlled by a parameter that goes from 0 to 100%. So not only do I need to drive the speed at which the animation goes from 0 to 100%, I need to set it on a loop so once it reaches 100%, it loops back to 0%. I'm extremely new to code so I don't think it would be wise for me to even provide a jumping off point, not that I could.

View 8 Replies View Related

C++ :: Sorting Some Data Based On Values Of A String Of Bits

Apr 1, 2014

I have a problem I am working on where I need to sort some data based on the values of a string of bits. The strings look like this,

010000001110000000

there are 18 bits, 1 means a feature is present, 0 means the feature is absent.

Each of these string has 4 on bits. I need to sort them such that I have the longest possible runs with 3 of the same on bits. It doesn't matter which 3 bits are on, I am just looking to order them in blocks with the longest possible runs. As a second step, the ordered blocks will be sorted by size large>small.

The following data is ordered like I need it to be.

Code:
// block 1, run of 12, keys 1,2,11 are identical (key 12 is also identical)
011000000001100000
011000000001100000
011000000001100000
011000000001100000

[Code] .....

This is the sort order that I am looking for. I need to be able to take a list of the bit strings in any particular order and sort them into the order above. The algorithm would need to recognize that there are 4 on keys and then look for groupings of three common on keys.

This is more of an algorithm question than one about specific implementation in code. I generally assume that most programming problems have been solved one way or another, so I don't know much about analyzing and manipulating strings of bits.

Is there a standard method for this kind of pattern recognition?

View 14 Replies View Related

C++ :: Create Matrix Of Data That Add Values Based On Reading Get From DVM

Apr 22, 2012

Ok I'm trying to create matrix of data that I can add values to based on a reading that I will get from a DVM. I'm using a 3d vector to do this. I need to keep track of the serial number, the day and within day I need to take two readings at different temps, this will continue for 7 days.

The rows of my 3d vector will be the days, the colums will be the serial numbers the depth will be the reading at the different temps.

What I need to do is compare the first element (days) and when it is greater then or equal to 4 I will perform calculations of the readings. So all I want to do is compare the first element of the vector, How do I do this?

Here is what I got

Code:
#include <vector>
typedef std::vector<double> Double1D;
typedef std::vector<Double1D> Double2D;
typedef std::vector<Double2D> Double3D;
#define HEIGHT 5
#define WIDTH 3

[Code]....

View 7 Replies View Related

C++ :: Federal Tax Calculator Based On User Input

Apr 17, 2014

I am doing an assignment that is to calculate the federal tax based on the users input. I am not entirely sure what it is doing, but it is definitely not what I want it to do. I commented out the loops because they seemed to have caused a problem, but there is still problems that are making the program do something else. Here is my code:

#include<iostream>
#include<string>
using namespace std;
double getData();
double taxAmount();

[Code] ....

Functions just seem like they are not my thing. It never gets to the second function to do the calculations and then return to main to display the results.

I tried changing the code to a switch(status) with a default: at the end that looped the "please try again" but for some reason, that was a fail, it created an endless loop.

View 15 Replies View Related

C++ :: Pixelate Image Based On User Input

Dec 24, 2014

My assignment is to pixelate an image based on users input. I have a structure that stores the image, and in this structure I'm storing the height and width and a dynamic array of RGBA struct.

This is my function. my main issue (that I've found) is offsetting and figuring out a way to handle the "edge" if the dimensions don't add up to the set pixelation size.

// @------------- Pixelation X
bool filter_pixelation(float percentageX, float percentageY, Picture& pic) {
// URL
/* 50% of 80 = (50/100) * 80 = 40 */
size_t perX = percentageX * (float)pic.width;
size_t perY = percentageY * (float)pic.height;

[Code] ....

Here is the getIndex function:

int getIndex(int x, int y, int width) {
return y * width + x;
}

View 1 Replies View Related

C/C++ :: Change Data Based On Users Input?

Feb 7, 2014

Player is given choice of x number of Races This choice changes the player's base stats. Then player is given a choice of x number of Classes (archer, warrior, etc.) This choice determines how the player's stats will change upon leveling up.

#include <cmath>
#include <iostream>
#include <ctime>

[Code]....

View 7 Replies View Related

C/C++ :: How To Draw A Square Based On User Input

Nov 5, 2014

Okay so I have to draw a square using "c" that draws according to the user input. Also, this has to be a function, and here is the code I have so far:

#include <stdio.h>
void DrawSquare(int length, char symbol);
void DrawSquare(int length, char symbol) {
int row;
int col;

[Code] .....

When I run this program it compiles fine but when the user input is recorded it just draws nothing but makes tons of spaces.

View 2 Replies View Related

C++ :: Create Simple Input Interface On Console - Allow To Input Values To Variables

Apr 6, 2013

I am trying to create a simple interface on console to allow to input some values to some variables. For ex:

int main() {
double a = 1.5;
double b = 2.5;
double c = 3.5;
string x;

[Code] ....

However, I want these three to display at the same time (now they display one by one), and in the console window I can move the cursor between input place of a, b and c with "arrow key" of keyboard.

View 2 Replies View Related

C :: Count How Many Negatives And Positives Are Based On Users Input

Oct 5, 2014

I just don't understand how you would have it count how many negatives and positives there are based on the user's input? Here's the problem and the code it provides:

Modify the program so that is displays the average of the positive and negative numbers. (Hint: be careful not to count the number 0 as a negative number.)

Code:

#include <stdio.h>
#define MAXNUMS 5
int main() {
int i;
float number;
float postotal = 0.0f;
float negtotal = 0.0f;

[Code]...

View 6 Replies View Related

C++ :: Making Diamond Out Of Asterisks Based On Given Odd Integer Input

Sep 6, 2014

I have been tasked with making a diamond out of asterisks based on a given odd integer input. For some reason the bottom half of my diamond will not print. I'm not sure as to why.

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

C++ :: How To Stop Outputting Data Based On A User Input

Sep 27, 2013

how to stop outputting data based on a user input. The text file is as follows:

1. a
2. b
3. c

and the code I'm using is as follows:

int main (){
string line;
int search;
cout << "Enter a number from 1-3" << endl;
cin >> search;
search++;
ifstream myfile ("example.txt");

[Code]...

What I want to do is to just output number 1 (the whole line) if the user enters number 1. However, I get an error on the second condition w/c is the "&& line!= search"

View 1 Replies View Related

C++ :: Dynamic Function Execution Based On String Input

Jun 27, 2013

I've got some functions and macros that I want to execute based on a string input that matches the function's name. I came across as this being a possible solution that I'd like to pursue IF it is possible to do. To clarify, I want to be able to look at a string and if the string matches the name of a defined function or macro then it will execute. Is there an effective way to do this (or is it even possible)?

It has to be pretty robust and dynamic given the project's purpose. Basically there's an input file that is being parsed and functions should execute if called upon in the file. I can't simply match strings to their corresponding functions in decision statements, as this isn't robust and isn't scalable.

View 3 Replies View Related

C++ :: Storing Multiple Arrays Based On User Input

Feb 7, 2014

I'm trying to create a function where it allows the user to type in multiple amounts of integers, so if the user wanted to have 3 different storages that hold different integers, the input would look something like this:

5
97 12 31 2 1 //let's say this is held in variable "a"
1 3 284 3 8 // "b"
2 3 482 3 4 // "c"
2 3 4 2 3 // "d"
99 0 2 3 42 // "e"

Since we don't know what number the user will input every time, I'm not sure how to create a dynamically allocated array that will create an x amount of arrays every time.. I want to be able to access each index of a, b, c, d, e or however many arrays there are.

So far, this is what I have, but I'm having trouble creating the arrays since it's unpredictable. I'm purposely not using vectors because I don't really get how pointers work so I'm trying to play around with it.

int* x;
int length, numbers;
cin >> length;
x = new int[length]
for (int i=0;i<length;i++)
{
cin >> numbers; //this doesn't work because it only grabs the first line for some reason
x[i] = numbers
}

View 3 Replies View Related

C/C++ :: Program That Calculate PI Based On User Input For Accuracy?

May 24, 2014

I am trying to create a program that will calculate pi based on a user input for accuracy. If the user input .3 then when the leibniz infinite sum value at a particular i becomes less then the input of .3 then the loop will exit.

I have looked at a number of examples on the internet but I feel lost. I have put together working code that will infinitely output sums but I need the loop to stop when the sum value is less then the accuracy value.

My question is what is wrong with my while loop, why will it only give me infinite summations? How do I make it so that the loop will exit when my accuracy input is greater then the sum?

int main () {
double accuracy;
cout<<"Give an accurate number." << flush;//looks nice
cin>>accuracy;
int d;//initialize denominator
double pi = 0.0;
while(accuracy < d){

[Code]...

View 8 Replies View Related

C++ :: Display All The Contents Of A File Excluding One Based On User Input

Mar 10, 2014

i want to display all the contents of a file excluding one based on user input say i have 4 records in the file numbered 1 to 4....the user chooses 2 to exclude it outputs records 1,3,4,4 when it should be records 1,3,4 what am i doing wrong here is the code.its basically displaying the last record in the file twice

void excludeRecord()
{
ifstream read;
read.open("Data.txt");
int recordC =0;
string fnameC = " ";
string lnameC = " ";

[Code]...

View 2 Replies View Related

C++ :: Generate A Report Based On Input Received From A Text File

Nov 5, 2014

Here is the assignment... Write a program to generate a report based on input received from a text file. Suppose the input text file student_status.txt contains the student’s name (lastName, firstName middleName), id, number of credits earned as follows :

Doe, John K.
3460 25
Andrews, Susan S.
3987 72
Monroe, Marylin
2298 87
Gaston, Arthur C.
2894 110

Generate the output in the following format :

John K. Doe 3460 25 Freshman
Susan S. Andrews 3987 40 Sophomore
Marylin Monroe 2298 87 Junior
Arthur C. Gaston 2894 110 Senior

The program must be written to use the enum class_level :

enum class_level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR } ;

and define two namespace globalTypes (tys and fys) for the function :

class_level deriveClassLevel(int num_of_credits) ;

The function deriveClassLevel should derive the class_level of the student based on the number of credits earned.

The first namespace globalType tys should derive the class level based on a two year school policy. And the second namespace globalType fys should derive the class level based on a four year school policy.

Four Year School Policy:
Freshman 0-29 creditsSophomore 30-59 credits
Junior 60-89 creditsSenior 90 or more credits

Two Year School Policy:
Freshman 0-29 creditsSophomore 30 or more credits

and this is the code I have so far...

#include <cstdlib>
#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;
enum class_level {FRESHMAN, SOPHOMORE,JUNIOR,SENIOR};
class_level classLevel;

[Code] .....

My main question is did I use the namespaces and enum correctly? And my second question is whats the best way to input the data from the text file? This is really where I get stuck.

View 3 Replies View Related

C++ :: Generate A Report Based On Input Received From Text File

Nov 7, 2014

Write a program to generate a report based on input received from a text file. Suppose the input text file student_status.txt contains the student’s name (lastName, firstName middleName), id, number of credits earned as follows :

Doe, John K.
3460 25
Andrews, Susan S.
3987 72
Monroe, Marylin
2298 87
Gaston, Arthur C.
2894 110

Generate the output in the following format :

John K. Doe 3460 25 Freshman
Susan S. Andrews 3987 40 Sophomore
Marylin Monroe 2298 87 Junior
Arthur C. Gaston 2894 110 Senior

The program must be written to use the enum class_level :

enum class_level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR } ;

and define two namespace globalTypes (tys and fys) for the function :

class_level deriveClassLevel(int num_of_credits) ;

The function deriveClassLevel should derive the class_level of the student based on the number of credits earned.

The first namespace globalType tys should derive the class level based on a two year school policy.
The second namespace globalType fys should derive the class level based on a four year school policy.

So I basically did it in parts and got everything working and then had to make the namespace so I had this:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };

[Code] ......

I know I have to clean it up and change it but it ran like it was suppose to. Then I tried adding the global namespaces and I this:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
class_level classLevel;

[Code] ....

with this i keep getting an error saying tys::deriveClassLevel: must return a value and tys::fys::deriveClassLevel: must return a value. I have been messing around with this part and struggling I thought I used the namespace to run the if statements with the criteria for the years of school. Basically I have been stuck for awhile and trying to change things around but I cant seem to get it to work.

View 2 Replies View Related

C/C++ :: Printing Square Pattern Of Asterisks Based On User Input

Dec 15, 2014

I have to make a program that displays a square of asterisks based on the users input. Here is what I have so far.

#include<iostream>
using namespace std;
int main(){
int number;
cout << "Enter a number: ";
cin >> number;
for (int i = 0; i < number; i++){

[Code] ....

But the square needs to be hollow like this. So if the user enters 5 then the following square will be shown.

*****
* *
* *
* *
*****

How to make it do this.

View 11 Replies View Related

C/C++ :: Create A Program That Prints A Certain Number Of Asterisks Based On User Input

Apr 12, 2015

I need to create a program that prints a certain number of asterisks based on user input. The user inputs 5 and I want my program to output "*****". How would I do this in C? I've tried printf("%#**", myvariable) but this does not work it only prints "*".

View 1 Replies View Related

C++ :: Generate Report Based On Input Received From Text File - User Defined Namespace

Nov 11, 2014

Write a program to generate a report based on input received from a text file. Suppose the input text file student_status.txt contains the student’s name (lastName, firstName middleName), id, number of credits earned as follows :

Doe, John K.
3460 25
Andrews, Susan S.
3987 72
Monroe, Marylin
2298 87
Gaston, Arthur C.
2894 110

Generate the output in the following format :

John K. Doe 3460 25 Freshman
Susan S. Andrews 3987 40 Sophomore
Marylin Monroe 2298 87 Junior
Arthur C. Gaston 2894 110 Senior

The program must be written to use the enum class_level :

enum class_level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR } ;

and define two namespace globalTypes (tys and fys) for the function :

class_level deriveClassLevel(int num_of_credits) ;

The function deriveClassLevel should derive the class_level of the student based on the number of credits earned.

The first namespace globalType tys should derive the class level based on a two year school policy.
and the second namespace globalType fys should derive the class level based on a four year school policy.

Four Year School Policy:
Freshman 0-29 creditsSophomore 30-59 credits
Junior 60-89 creditsSenior 90 or more credits

Two Year School Policy:
Freshman 0-29 creditsSophomore 30 or more credits

NOTE : use ignore() function with ifstream objects whenever you want to ignore the newline character.

For example :
ifstream transferSchoolFile ;
transferSchoolFile.open("student_status.txt", ios::in);

while( !transferSchoolFile.eof()) {
getline(transferSchoolFile,name) ;
transferSchoolFile >> id >> credits;
transferSchoolFile.ignore(); //Used here to ignore the newline character.
….
}

I did this in parts so I got it working with a four year criteria without the user defined name spaces.

include <iostream>
#include <fstream>
#include <string>

using namespace std;
enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };

[Code] ....

So that worked fine then I tried with name spaces -

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
enum class_level { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
class_level classLevel;

[Code] ....

I know I have some stuff to mess around with but I am currently stuck with two errors, first -

Error1error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartupC:UsersstephenDocumentsVisual Studio 2013ProjectsinputConsoleApplication1MSVCRTD.lib(crtexe.obj)ConsoleApplication1

then -

Error2error LNK1120: 1 unresolved externalsC:UsersstephenDocumentsVisual Studio 2013ProjectsinputDebugConsoleApplication1.exeConsoleApplication1

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved