C++ :: Mathematical Operations With Digits In Strings?

Feb 11, 2015

I'm working on a class project, and I'm having a difficulty. Suppose I have: string a = "21" and string b = "30"; normally, a+b=2130 (i.e concatenation of the characters in the string) but suppose I want a+b=51 (i.e. numerical addition) how do I go about this?

View 1 Replies


ADVERTISEMENT

C++ :: Program To Complete Mathematical Operations Using Matrices

Jul 10, 2014

I've been assigned to build a program which completes mathematical operations using matrices. I have to use dynamically allocated 2d arrays, and can only use * to dereference and not []. I have this code so far, but the multiply_matrix function does not work with most values. I've looked at other similar posts, which have a similar algorithm to mine, but mine does not work for some reason.

/*
*(*matrix(matrix+i)+j)
*/

#include <iostream>
//for sleep() which allows user to see messages before screen is cleared
#include <unistd.h>
using namespace std;

[Code] .....

View 1 Replies View Related

C++ :: Changing Integer Into New Integer With Simple Mathematical Operations?

Jun 15, 2014

changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).

View 4 Replies View Related

C++ :: Input Integer Then Output Both Individual Digits Of The Number And Sum Of Digits

Oct 11, 2014

My problem needs to prompt the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. An example would be entering 8030 and it spits out 8 0 3 0 as well as 8+0+3+0=11 and it needs to work with negative numbers.

Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int base;

[Code] ....

Now I don't know if any of this is right a hint my professor gave us is that to get the fourth digit you would do base mod 10 and to get the first digit you do base divided 1000...

Code:

{
int power;
int counter=0;
int value=1;
cout << "Enter the power of 10 you want: ";

[Code] ....

View 2 Replies View Related

C# :: Using Mathematical Functions

Dec 5, 2014

like other programmers I am also new to the world of programming. So, I have to use a mathematical function in Cmath.

The function is z1 = Sin2α + sin5α - Sin3α / cosα+1-2sin*2(2α)

z2= √x*3 + 3 / x*3 - 3
My results so far...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

[Code].....

View 6 Replies View Related

C++ :: Take (mathematical) Inverse Of Array?

Feb 17, 2014

I need to take an array, and get the inverse of it (basically, just how you would take an inverse of a function in math). I need to do it where, if a[i] = x, b[x] = i. I would just copy from array a to array b in a function.

View 10 Replies View Related

C++ :: Mathematical Library - OOP Linker Errors?

Apr 21, 2014

I've just started coding a "Mathematics" library for my own, just to practice some OOP concepts, but sadly I didn't get too far with it before the first errors appeared. That is, I created a Matrix.H and Matrix.CPP file (separate class) in a "Linear Algebra" folder.

However, when I run the code I get the following (linker?) error:

#include <iostream>
#include "Matrix.h"
using namespace std;
int main() {
Matrix<int> A(7,5);
int row[] = {5, 10, 9, 11, -5};

[Code] ....

I am using Visual Studio 2012 (stating this in case it could be related to the reason for which I get the error).

View 2 Replies View Related

C++ :: Adding Mathematical Expression In Binary Tree

Jul 23, 2013

I want to add mathematical expression in a binary tree but I have some problems with the algorithm. I found this one:

-If the current token is a '(', add a new node as the left child of the current node, and descend to the left child.
-If the current token is in the list ['+','-','/','*'], set the root value of the current node to the operator represented by the current token. Add a new node as the right child of the current node and descend to the right child.
-If the current token is a number, set the root value of the current node to the number and return to the parent.
-If the current token is a ')', go to the parent of the current node.

Here is the code that I made so far:

template<class T>
void Tree<T>::Expr(Node<T> *node, char expr[], int &i) {
i++;
T x = expr[i];
if(x == '(') {
node = node->Left;

[Code] ....

I know that it is a big mess and it doesn't follow the algorithm but this is the problem. For example if the token is '(' I go to the left child of the current node. Then lets say that the next token in the expression is a number. I add this number to the current node and I must go back. But how can I go back to the parent? I will go back to line 13 and the program will end. What should be the structure that I must use?

View 1 Replies View Related

C/C++ :: Tokenize Mathematical Expression - Garbage Value In String

Feb 14, 2014

I wrote a program that tries to tokenize a mathematical expression, inserting the tokens in a list of strings. The list is as follows:

typedef struct listOfStrings {
    char **array;
    int size;
} ListOfStrings;

There is even a function to initialize the listOfStrings. The thing is: I'm printing a token every time it is complete and every time it is inserted in the list. The output is okay. However, when all tokens are processed and I call function print_list_of_strings to print the tokens again, the first token is printed with a leading garbage value if the input for the program is "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3". How is this possible? The code for printing the list is as follows:

void print_list_of_strings( const ListOfStrings *const lPtr ) {
    int i;
    int numberOfElements = lPtr->size;  
    if ( numberOfElements != 0 ) {
        for ( i = 0 ; i < numberOfElements ; ++i ) {

[Code] ....

The list just prints --- if it's empty, although this isn't the case for the program I'm writing. Also, if the input is "1 + 2", everything goes fine. The code for inserting at the list is:

int insert_at_end_of_list_of_strings( ListOfStrings *lPtr, const char *const str ){
    int lengthOfStr = strlen( str );
    int numberOfElements = lPtr->size;  
    if ( ( ( *( lPtr->array + numberOfElements ) ) = ( char * )malloc( ( lengthOfStr + 1 ) *
 
[Code] ....

View 5 Replies View Related

Visual C++ :: How To Print Mathematical Series On Screen

Nov 24, 2013

How do I print a mathematical series such as

1 + 2/2! - 3/3! + ...... n/n! //n is read from user

Not only we need to print the series on the screen but at the same time find the sum using simple loops.

View 1 Replies View Related

C++ :: Convert A String Into Mathematical Calculation And Calculate In Correct Order

May 6, 2013

I want to program an advanced calculator. I'd like to enter some more complex expressions like -17+3*4*(4-sqrt(4) and i want, that mathematical operations are done the correct order, so at first 4-sqrt(4) is calculated, then 3*4*2 and then -17 is subtracted.

Problem 1: Convert a string into a mathematical calculation
Problem 2: Calculate in the correct order

How would I do that (I dont expect perfecly precoded calculators from you, just the way how to do it)

Google search just delivers primitive calculations with entry methods like

Enter first number 1
Enter operator +
Enter second number 2

3

But thats not what i want

View 2 Replies View Related

C++ :: Evaluate Mathematical Expression That Is Already Formed In Binary Search Tree

Apr 23, 2013

The point of my code is to evaluate a mathematical expression that's already formed in a Binary Search Tree. : ((6+5)-3)*(2/1)=?

I've read other forums and they usually use cout, cin and << >> but I'm still in the learning process so this is basically primitive coding.

Anyway, The recursion is already correct (I think). My problem is how to return char* or should I say an element which has 2 or more digits.

typedef struct cell{
char elem[max];
struct cell *left;
struct cell *right;
}celltype, *node;
node A;
char* evaluate(node *A) //passed as evaluation(&A);

[Code] ....

And the reason that I'm using char is because the operators are also elements in different nodes.

The code above is actually the result when I remembered that you can't return 2-digit char.

The code below is before I remembered that. It works when all results are 1 digit numbers.

typedef struct cell{
char elem;
struct cell *left;
struct cell *right;
}celltype, *node;
node A;

[Code] ....

View 1 Replies View Related

C++ :: Operations On Enumerations

Mar 12, 2014

I'm used to writing enums for enumerated types. However I have this:

enum Colour
{
BLUE = 0x01,
BLACK = 0x02,
WHITE = 0x04,
GREEN = 0x08,
RED = 0x10
};

Now each of the enumerated types are not exclusive, so I'd like to do this:

Colour c = BLUE | BLACK;

Of course that doesn't work. gcc gives "invalid conversion from 'int' to 'Colour' [-fpermissive]".

typedef int Colour;
static const Colour BLUE = 0x01;
static const Colour BLACK = 0x02;
static const Colour WHITE = 0x04;
static const Colour GREEN = 0x08;
static const Colour RED = 0x10;

View 2 Replies View Related

C++ :: Nested Conditional Operations

Feb 4, 2014

So I think I am having syntactical problem with my code.

Code:
int main() {
vector<int> ivec;
int score;
[Code] ....

I get an error from my compiler on the ?10th? line (Nested condition line) that says |19|error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'|

The purpose of the program is to take input and store it in a vector and then change the value to be between 1-6. I made this for the purpose of learning about nested conditional operations.

View 3 Replies View Related

C :: Floating Point Operations

Mar 16, 2014

Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float i;
i=0.7;

[Code] ....

If i do run the above program in turbo C/C++ complier, it outputs "h". But,if i change the code as i=0.6 and if (i<0.6), it outputs "w". Even if i change it to i=0.8 and if(i<0.8), then also it outputs "w".

View 4 Replies View Related

C++ ::  bitwise Operations On Numbers

Dec 8, 2014

Trying to write 4 bytes ints in a binary file and extract them after... I'm using the exclusive or (^) to isolate single bytes to write to and extract from the file since the write() function accepts only chars, only the beginning and end results are not the same...

#include <iostream>
#include <ctime>
#include <fstream>
#include <cstdlib>
using namespace std;

[Code] .....

View 3 Replies View Related

C++ ::  how To Handle Async Operations

May 29, 2013

I've been thinking over this for long time... For example, when a button is clicked, print a string "clicked" after 5 seconds. (the button won't be blocked)

Thread should be the only way to do this:

btn.on_click = []() {
thread thrd([]() { sleep_5_seconds(); cout << "clicked" << endl; });
thrd.start();
};

the local variable thrd is destructed right after it starts, which may cause a crash, so use new operator:

btn.on_click = []() {
thread* thrd = new thread([]() { sleep_5_seconds << "clicked" << endl; });
thrd->start();
};

The thrd never get deleted

How would you solve problem like this?

View 2 Replies View Related

C++ :: Logical Operations Not Working?

Oct 7, 2014

int main()
{
char rORc, choice;
int sizerc;

[Code]....

when I input the character which is the underscore, and the row # it should display the table and sort that specific row. Why is the if statement skipped? This is not the complete program but has everything needed.

View 2 Replies View Related

C++ :: Validate Unsigned Operations?

Oct 12, 2012

I've sometimes encountered unexpected runtime issues caused by unsigned values being decremented below zero.

Example 1: "unsigned_value += negative_integer_value;"
Example 2: "for( size_t i = size - 1; i >= 0; --i )"

My compiler doesn't provide any compile-time or run-time warnings.

As far as I know, it's not possible to overload operators of primitive data types to check if the unsigned value is decremented below zero.

Any clever strategy to trace such cases at debug runtime, without having to add asserts all over the code? It's important that it does not affect performance in release mode.

View 6 Replies View Related

C++ :: Variable Length Integer Operations?

Aug 25, 2013

I intend to reference this thread from another thread so I do not clutter that thread with code

/* This code is relied on by some of the functions I will post */
typedef unsigned long long zxumax;
typedef signed long long zxsmax;

[Code]....

View 13 Replies View Related

C/C++ :: Console Input / Output Operations

Oct 27, 2012

How we can write coloured text to a text file in c? Can I use cprintf funtion with any kind editing

View 1 Replies View Related

C++ :: Incorrect Result In Bitwise Operations?

Oct 30, 2014

I'm doing a bitwise operations on 2 bytes in a buffer, then storing the result in a variable. However, I sometimes get a non-zero value for the variable even though I'm expecting a zero value.

The relevant portion of the code is as follows.

unsigned int result = 0;
long j = 0, length;
unsigned char *data;
data = (unsigned char *)malloc(sizeof(unsigned char)*800000);

[Code] ......

I'm expecting result to be zero when my data[j] and data[j+1] are 0xb6 and 0xab respectively, which is the case for most of the time. However, for certain values of j, my result is strangely not zero.

j = 62910, result = 64
j = 78670, result = 64
j = 100594, result = 64
j = 165658, result = 512
j = 247990, result = 128
j = 268330, result = 512
j = 326754, result = 1
j = 415874, result = 256
j = 456654, result = 1024
j = 477366, result = 512

It appears that these strange result values are all powers of 2, with a 1 bit appearing somewhere in the unsigned int.

I'm not changing the value of result anywhere else in the code, and when I print out (unsigned int)(((data[j]^0xb6)<<8)|(data[j+1]^0xab)), I get 0, but somehow when it gets stored in result, it's no longer zero.

View 3 Replies View Related

C :: Losing Contents Of A String Between File I/O Operations

Oct 24, 2013

Here's the code I'm writing:

Code:
#include <stdio.h>
#include <stdlib.h>
char* getTeamCode(char* team);

[Code]....

Why the placement of the code on line 21 above matters.

It grabs the correct string just fine. If I write a printf just below it, it prints the string it should correctly. However, if I do a printf of the string OSUteamCode below the fopen call on line 23, it prints blank.

So the first thing I did was move it below the fopen line. It worked, finding and outputting the first game in "game.csv" just fine, but not the other 11. Debugging with printf shows that the contents of OSUteamCode again disappear after the fopen call in the addLineCSV function.

I'm not understanding why that happens. The only thing I can figure is there's something going on with the file I/O commands that I just don't understand, but I can't find anything online that explains what that might be.

View 3 Replies View Related

C++ :: Limited Purpose Calculator Which Finds Value Of One Of Five Operations

Jan 9, 2015

I just started learning C++ a week ago and have been stuck on a project for the past 2 days now. I am building a limited purpose calculator which finds the value of one of five operations. Visual studio doesn't underline any errors in my program but every time I try to run it I get an error message. I believe it has something to do with the if/else but Im not sure.

#include <iostream>
using namespace std;
int main(){
int a;
int b;
int sum = a + b;

[Code] ....

View 5 Replies View Related

C++ :: Perform Arithmetic Operations With Template Parameters?

Jul 13, 2013

how to use template parameters to perform arithmetic operations on objects.

I feel that it would best to demonstrate my issue rather than try and explain it.

Sample:

// Fundamental object structure
template<int T> struct myInt
{
myInt() { value = T; };

[Code]....

What I don't know is how to get a hold of the T variable to add them through the 'add' structure. Also, might any of this have to do with sequence wrappers?

seq_c<T,c1,c2,... cn> is essentially what I'm thinking of. Where T in this case is the type and c to the nth c are the values.

View 4 Replies View Related

C :: Point Operations On Elliptic Curve In A Prime Field

Apr 21, 2013

I am trying to write a program to perform point operations on a elliptic curve in a prime field I am using the standard formulaes for point additions and doubling in my code and these operations are performed by functions that are called but I am getting output for certain points but not all.

Code:

structure point_multiply(int x, int y, int k ) {
int xk;
int yk,m;
xk=x;
yk=y;
m=1;
int xL,yL,s,e;

[code].....

s1, s2, s3 etc are structures which hold a 2 integers which act as x and y co-ordinates.I am getting output by entering k=3,g=4, h=5 and many other cases mostly with small numbers but not for larger numbers.

View 3 Replies View Related







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