C++ :: Function That Subtract Value From Each Character In Array

Mar 17, 2013

I have to write a function Subtracts val from each character in the array

// If the array is: abcdefghi and value = 5, then result is: 5 ascii characters subtracted from the given array
// note: Make sure the character remains in the ' '
// to '~' (inclusive)
// If character < ' ', then add 95
// If character > 126, then subtract 95
// to put back within the range, and repeat as needed
// all printable characters are between this range
// ' ' is the 1st printable character (32)
// '~' is the last printable character (126)
note: subtracting values from the char will cause an overflow.

the prototype is: void subtracting(char array[], int size, int value)

View 3 Replies


ADVERTISEMENT

C++ :: How To Subtract A Set Value From Each Character In Array

Mar 16, 2013

this function subtracts a given value for each element in an array. I If the array is: hellohowareyou, and the value is 6 then result will be : 6 characters from each letter in the initial array

and the prototype of it is:

void minusFromArray(char arr[], int size, int value);

how do i convert each element in the array into ascii and subtract the values from those ascii characters?

View 1 Replies View Related

C :: Trying To Fill A Character Array With Scanf Function

Oct 5, 2014

I'm trying to make a character array, and be able to custom fill it by using the scanf() function to ask the user for each character spot in the array.

Code:
#include <stdio.h>
int main() {
int i;
char character_array[11];
char *charpointer;
charpointer = character_array;

[Code] .....

And this is the output i get when i run it:

Code:
root@kali-Tulips:~# ./myown
what letter would you like in the [0] spot of the array? :
a
what letter would you like in the [1] spot of the array? :
what letter would you like in the [2] spot of the array? :
b
what letter would you like in the [3] spot of the array? :
what letter would you like in the [4] spot of the array? :

[Code] .....

Obviously I'm trying to grasp C programming and am very new. I don't understand why this isn't working, and i think its more than my lack of knowledge of C syntax. I believe the correct memory is allocated but when examined with gdb i don't find what i expect....

View 4 Replies View Related

C :: Transferring Contents Of One Character Array To Another Function

Feb 1, 2014

In the function *expandArrayList and *trimArrayList I need to allocate either more memory or less memory to the given array. Right now I am using malloc on a new array and then transferring the elements of the original array to the new array, freeing the original array, and then reassigning it to the new array so it's size and capacity are either bigger or smaller than the original. I feel like there has to be a simpler way to do this with either realloc or calloc, but I cant find sufficient information on either online. Would I be able to use either to make this easier and the code cleaner?

Also, these are character arrays that contain several character arrays inside them, so I was wondering what the best way to transfer the contents of one array to the other would be. I have gone between

Code:

//example
for (i=0; i<length; i++){
newarray[i] = oldarray[i];
}
and
Code: for (i=0; i<length; i++){
strcpy(newarray[i], oldarray[i]);
}

but I'm not sure which one (if either) should work.

The 'ArrayList.h' file that is included contains the structure ArrayList, as well as the function prototypes for the functions listed below.

Here is the structure in ArrayList.h:

Code:

#define DEFAULT_INIT_LEN 10
typedef struct ArrayList {
// We will store an array of strings (i.e., an array of char arrays)
char **array;

[Code] ....

Here is my code:

Code:

//included libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ArrayList.h"
//Create arraylist

[Code]....

View 3 Replies View Related

C :: Function Which Takes A Character Array And Reverses It

Dec 7, 2013

Write a c function which takes a string(as a character array, null terminated) and reverses it. it doesn't print the reversed string -- it modifies the given string so that it is reversed.

Code:

void reverse(char array[]) {
char tmp;
int i, j;
for(i=0;i<strlen(array); i++){
for(j=strlen(array)-1;j>=0;j--) {
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
} } }

how come this doesn't work?

View 2 Replies View Related

C :: Assignment Of Element Of 2D Character Array To 1D Character Array

Jul 4, 2014

Can we do this :

Code:
char strings[][100]={"ABC","EFG","IJK","LKM"};
char temp[100];
temp=strings[1];

View 3 Replies View Related

C :: How To Subtract Evenly

Feb 20, 2015

how do I subtract this so that I use the smallest number of $20, $10, $5, and $1 to pay $93? The book tells me to "Divide the amount by 20 to determine the number of $20 bills needed, and then reduce the amount by the total value of the $20 bills. Repeat this for the other bill sizes."I think the part in bold throws me off. What IS the total value of the $20 bills if I use $93 as the example? Is it $80, the highest we can go with $20 bills? Is it 4, the number I got from 93/20 after cutting off the decimal places, also the total number of $20 bills I can use?

I know how to do it and can do it in real life. It's like, "Oh, I need to pay $93. So I use four $20 bills, which brings me to $80. Using one more would bring it to $100, which is too much. So now, I need to use a $10 bill, which brings it to $90. Oh, so now, I need to use three $1 bills, which brings me to $93."But how do I tell that to a computer? It seems like I would have to put some kind of limits on it, or string code lines together,Here is the code so far:

Code:

#include <stdio.h>int main(void)
{
int amount, twenty, ten, five, one;
printf ("Enter a dollar amount:
");
scanf ("%d", &amount);
}

[code]....

View 5 Replies View Related

C :: Subtract Two Time Intervals?

Mar 12, 2014

I am looking for simple code that subtract two time interval. I have time t1=5hour 30 minute and other time in 24 hour format. This code i written but it not working as expected. it not printing 5:30 minute subtract

Code:
main() {
intTime1;
intTime2;
int hour=10;
int minute=5;
int second=13;
int h;int m;
doubleNtime;

[Code] ....

View 3 Replies View Related

C++ :: Subtract In A String With Decrementation

Nov 24, 2013

Here's my code :

>>>>>
std::vector<std::string> x = split("3 5", ' ');
int total = 0;
// then we loop over the elements
for(size_t i = 0; i < x.size(); ++i) {
// convert the string to an integer
int n = atoi(x[i].c_str());
total = total + n;
}
std::cout << "total = " << total << std::endl;
}
<<<<<

So, as you can see, this will add 3 to 5. However, I would like it to do the inverse (3 - 5). How I can do that?

View 2 Replies View Related

C/C++ :: How To Subtract Two String Containing Numbers

Mar 29, 2014

I have a code for subtract of two char arrays(size of each is 50).but i want to write this program with string and i should be consider that size of each string may be not equals.for example we have s1=4777 and s2=55.and we should be subtract them. This is my code.

char* sub(char x[50],char y[50]) {
int i,j,temp=0;
char z[50]={'0'};
for(i=0;i<50;i++)

[Code] .....

View 9 Replies View Related

C++ :: Add And Subtract Two Sets Of Integer Arrays?

Apr 24, 2013

Ok so I am currently trying to add and subtract two sets of integer arrays. When I run my program, the program does not subtract the numbers from the first set with the numbers from the second set. Could anyone here take a look at my code and help me figure out what the problem is?

#include <iostream>
#include "SafeArray.h"
using namespace std;

[Code].....

View 1 Replies View Related

C :: Why Program Can't Subtract Negative Number From Positive One

Feb 2, 2015

My program uses a while loop to eventually get to an error of zero and a root of sqrt(3). I'm not understand why after the third iteration the program fails to compute a new x value. I'm using Visual Studio 2013. The code tag instructions were dubious.

Code:

#include <stdio.h>
#include <math.h>
main() {
/*This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots.*/
printf("This program uses the Newton-Raphson method to solve y = (x^3)-3 for it's roots. Enter your estimate of the root.
");
float x,y,z;
int num;
num = 0;

[Code]...

View 1 Replies View Related

C :: Store Character Array In Reverse Order Then Display Reversed Array

Jun 14, 2013

The program should store a character array in reverse order then display the reversed array. I have also included in the code that will display the actual characters into the array as it loops through. So I know the characters are being stored, but why doesn't it display the entire string when I call it?

Code:
#include<stdio.h>
int main(){
char str[50];
char rev[50];
printf("Enter Desired Text: ");
scanf ("%[^

[Code] ....

Is it just my compiler?

View 5 Replies View Related

C++ :: Program That Add / Subtract / Multiply Or Divide Two Integers - Incorrect Answers

Oct 7, 2012

Create a program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtractions, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one."

And here is the test data. I am posting the results from my desk-check table.

operation first integer second integer answer
A 10 20 30
a 45 15 60
S 65 50 15
s 7 13 6
G -1
M 10 20 200
d 45 15 3
d 50 100 2

Then, I transferred my program into a source file. Here it is:

//Exercise16.cpp - display answer of two integers

#include <iostream>
using namespace std;
int main() {
//declare variables
int firstInteger = 0;

[Code] ....

After putting in the data, everything worked fine, except the last two operations, which are M (multiplication) and D (division). All the answers for the last two operations essentially give me a 0.

View 4 Replies View Related

C++ :: Calculator That Takes Two Large Numbers In Linked List Then Ask User To Add Or Subtract

Dec 30, 2012

What I'm trying to do is make a calculator that takes two large numbers in a linked list then ask the user if they wish to add them or subtract them.

#include<iostream>
#include<string>
using namespace std;

class Node {
public:
int value;
Node * next;

[Code] ....

View 5 Replies View Related

C/C++ :: Testing Character Array Against Array Of Characters?

Apr 16, 2014

I am currently having an issue with validating user input for a state abbreviation. I have an array where a list of abbreviations is stored to use as a comparison for whatever the user inputs. I have been able to get the list loaded properly but whenever i go to compare, it always comes back as true even if it isn't. Here is some relevant code:

static char stateTable[STATE_TABLE_SIZE][STATE_SIZE];
int main() {
char buffer[40], *testCustName[40], testState[5], testCode;
buffer[0] = '';
int quit = 0;
int p = 0;

[code].....

View 6 Replies View Related

C++ :: Multiple Choice Function - Using Character As Parameter

Feb 20, 2012

I have a multiple choice function below that is part of a larger program but I am having trouble having it operate with characters as inputs and arguments. It has to be a character input, but I kinda want it to act like an integer, how do i do that?

Code:
void addition(int *Preward) //function that is called by the users selection {
int random1, random2;
int order;
int one, two, three;
int rando1, rando2;
char a, b, c; // ERROR

[Code] ....

View 2 Replies View Related

C++ :: 2-Dimensional Character Strings And Realloc Function

Jan 19, 2012

The following code fails/crashes in the second loop where it prints the content and frees the memory. But the issue could be with the first loop.

int main(int argc, char *argv[]){
char **a, buf[80];
int x;
a = (char **)realloc(NULL, sizeof(char *));
for(x = 0; x < 5; x++) {

[Code] .....

View 3 Replies View Related

C/C++ :: Missing Terminating Character And Int Main Function Error

Oct 6, 2014

I am suppose to make a program that when the user is asked "Enter a Letter for the day:" if the user enters M or m then the screen will output "The day of the week is Monday" and so on until Sunday. I looked over my code and everything looks right but I still get errors saying Missing terminating character " and int function main error.
 
#include <iostream>  
using namespace std;  
int main() {      
    char day;  
    cout << "Enter a letter for a day of the week: ";

[code]....

View 1 Replies View Related

C :: How To Move A Character In 2D Array

Jan 27, 2015

I want to move a character in a 2D array This Character should move vertically in a 2D Array To exemplify it for Exam in Snake Game A character automatically moves Please Write a example code that works

View 4 Replies View Related

C++ :: Repeat Character Array

Nov 1, 2013

I'm having a little trouble writing program that on remove all repeat characters and leave only the non repeated characters. This is want I have so far.

#include <iostream>
#include <cstring>
#include <string>
unsigned repeatingCharCount(char const* s) {
std::string unique ;
const unsigned length = std::strlen(s) ;

[Code]...

View 6 Replies View Related

C++ :: Floating Value From Character Array

Dec 9, 2014

How to extract a floating value from a character array in separate variables .

Example : multiply 7.5 with 2

Out put : 7.5 , 2

View 1 Replies View Related

C++ :: How To Delete First Character In Array

May 1, 2014

How do i delete the first character in a character array?

I want output to be

void DeleteFirst (char S[ ])
{
for( int i = 1; i < MAX_LENGTH; i++)
cout << S[i];
}

i am just writing the wrest of the Array without deleting the first. how do i delete the first?

View 7 Replies View Related

C++ ::  add A Space Into Character Array

Apr 29, 2014

I have a character Array 'Hello'

I want to add a space in whatever space the users chooses.

"Where do you want to add space? --> " 3 String after space is added --> 'Hel lo

View 4 Replies View Related

C/C++ :: Clear First Character Of Array

Mar 26, 2014

I am trying to clear the first character of my array. The first two methods don't seem to work. The third method seems to but doesn't look like a good idea.

char operand[] = "abc";
//both methods seem to null it out
operand[0] = 0;
memset(operand, 0, 1);
printf("%s", operand);

//seems to work
memset(operand, 5, 1);

View 4 Replies View Related

C++ :: Making Constructor For Array Of Character

Jan 16, 2015

Code:
class Robot{
char name[15]; //assign to char n
int x, y , direction;
void ToString(int d);

[Code]......

how do i assign char name[] in the class to char n in the constructor?

View 3 Replies View Related







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