C/C++ :: How To Avoid Replacing Element In Array

Oct 6, 2014

I am working on a parking lot scenario project using multidimensional arrays. The parking lot has 8 rows and 10 parking spaces in each row. Altogether there are suppose to be 30 cars parking in the lot and arrive in numerical order. I am suppose to generate a random number to represent the row and another to represent the space in the row.

The problem I have is that a few of the elements are being replaced. The project requires the car to check if the space is taken, if so it is to find another one. Here is what I have...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 8
#define COL 10
#define CARS 30
void parkTheCars(int result[ROW][COL]);
void displayArray (int result[ROW][COL]);

[Code] .....

View 1 Replies


ADVERTISEMENT

C++ :: Can Avoid Class Using Array Operator?

Feb 7, 2014

Imagine these simple class:

Code:
class test {
public:
void write(string a) {
cout << a;
}
};

(these class wasn't tested... it's for these question)... Can avoid the class use the array operator('[]')?

View 7 Replies View Related

C++ :: Replacing Word In Char Array

Jul 29, 2014

I'm writing a function that accepts a char array containing the title of a book as parameter, the following then has to take place:

1.) Extra white spaces between words need to be removed [DONE]

2.) Text has to be converted to title case, i.e each new word has to start with a capital letter [DONE]

3.) Lastly I have I text file (minors.txt) containing a number of words that should not be capitalized by the function, like "a" and "an", however I don't know how to implement this.

Example of end product:

ENTER THE TITLE OF THE BOOK: a brief hisTOry OF everyTHING

Correct Output:

bool Book :: convertToTitleCase(char* inTitle) {
int length = strlen(inTitle);
bool thisWordCapped = false;
//Convert paramater to lower case and
//Remove multiple white spaces
for (int x = 0; x < length; x++)

[Code]...

I was thinking of maby reading the words in the text file into a string array, and then comparing the two arrays to ensure that when a word is present in a text file, the word is not capitalized, however I don't know if that is possible between a string array and a char array.

View 3 Replies View Related

C :: Segmentation Fault When Replacing A Char In Array

Jan 25, 2013

This function should replace all instances of a character in a given character array, while returning the amount of characters changed, but I keep getting a segmentation fault at the highlighted area.

I'm only supposed to use pointers so arrays are out of the question, and I don't think we are allowed to use the string.h library as well. How I could avoid something the segmentation fault or ways to fix it?

Code:

int replaceChars(char replace, char find, char *input) { int i, j;
//Finds length
for(i = 0; *(input + (i + 1)) != ''; i++);
int c = 0;
for(j = 0; j <= i; j++) {
if(*(input + j) == find) {
*(input + j) = replace; //Segmentation fault happens here
c++;
} }
return c;
}

View 5 Replies View Related

C :: Replacing Individual Chars In A String Array?

Nov 16, 2013

I'm trying to right the function that puts the correct letters in the right positons being reported from my previous compareletter function for my game of hang man and its not updating the word in progress array. Here's my compare letter function that's working correctly:

Code: //function that returns the index of the letter that the user has guessed or Code: //-1 if the letter isn't in the word
int CompareLetter(char array[], char guess, int numLetters)
{
int i;

[Code....

However, this isn't changing any of the position of the asterisks in the word in progress array positions 0-3.

View 7 Replies View Related

C :: Reads And Compress Each Row Of Array By Replacing Each Character

Mar 1, 2014

Code for c programming reads and compress each row of the array by replacing each character with a single character and the number of times it occurs?

I have issue in doing this.

View 5 Replies View Related

C++ :: Linked List - Function Which Deletes Element If Next Element Is Bigger

Mar 10, 2014

So I have linked list and function which deletes element if next element is bigger, so my code is working but its not working with first element, in the comment I have wrote code which I would code for checking that first element, but when ever I check it is blowing up all program.

#include <iostream>
using namespace std;
struct llist {
int x;
llist *next;

[Code] .....

View 1 Replies View Related

C :: How To Avoid Negative Output

Jul 12, 2013

I have a c program that I partially have working. The problem is basically writing a program that allows the user to input the amount of calories they plan to eat a meal and disperse the calories from top to bottom. My program produces the output in the example if I enter 1050 but the issue I noticed if the number of calories is just enough to cover the burgers I get negatives in the other variables.

For example, if I enter a total amount of calories of 1050, I can eat: Output: 2 burgers @ 770 calories (1050 - 770 = 280 calories remain) 1 bag of pretzles @ 170 calories (280 - 170 = 110 calories remain) 1 pear @ 80 calories (110 - 80 = 30 calories remain) 6 tsp. ketchup @ 30 calories If I input 1050 I get the above output but if I input a different integer such as 2000 this is my output 5 burgers @ 1925 calories 0 bag of pretzles @ 0 calories -1 apple @ -80 calories -35 tsp. ketchup @ -175 calories I can't give the full code since this assignment holds a lot of points and was up all night getting it work.

So I'll provide pseudocode

define all 4 variables burger 385, pretzel 170, pear 80, ketchup 5 print out text How many calories can you eat prompt user input
Divide user input into burger How many burgers can bet eaten subtract calories eaten from original user input
Divide calories left into pretzel How many bags can bet eaten subtract burger calories from pretzel calories
Divide calories left after preztel into pear How many pretzels can be eatn subtract pretzels calories from pear calories
Divide calories left over into ketchup how much ketchup can i use show on screen (int total)of burgers @ (int calorie total) calories show on screen (int total)bags of pretzels @ (int calorie total) calories show on screen (int total)pears @ (int calorie total) calories show on screen (int total)teaspoons of ketchup @ (int calorie total) calories

The problem I see is that subtracting the calories from the pear from the left over calories of the pretzel calories leads to a negative. If leftover calories minus 80(pear int) its less then 0 . The calculations from the pear onward to ketchup become incorrect resulting in negative output.

View 6 Replies View Related

C/C++ :: Avoid Using The Goto Function?

Dec 17, 2014

I've been making an RPG in C++ and I've used goto all throughout the program. After receiving an error and Googling how to fix it, I read that you shouldn't use goto. What can I use as a instead of goto, which isn't 'considered bad programming practice'?

View 9 Replies View Related

C++ :: How To Avoid Redundant Code In Classes

Sep 4, 2014

ISSUE: How to avoid redundant code in c++ ?

Problem:Say I have a Base class called Car and it has 3 derived classes say Ford, Honda and Audi.

now the issue is all 3 derived classes have exactly same code but minor difference in calling member functions of their r respective engines( these engines have separate class) . example ford class calls code which is exactly same as other 2 classes but do call something like ford_engine-> start() ford_engine->clean() bla bla bla .

//ly honda calls honda_engine->start() honda_engine->clean();

//ly Audi calls audi_engine->start() audi->clean();

now here the issue is it has redundant code in all 3 places with minute difference. Hoow I can have code at one place most probably in Base class and all derive classes uses same code.

View 1 Replies View Related

C++ :: Way / Pattern To Avoid Copying Data

Apr 19, 2013

I have a class buffer, which holds a std::string as member, and a socket_receive function:

struct buffer {
string data;
buffer() {}
buffer(buffer& b) : data(b.data) {}
};
buffer socket_receive() {
buffer tmp;
tmp.data = "1234";
return tmp;
}

so when I write

buffer b = socket_receive();

there is a copy constructor, and the tmp variable is constructed and destructed, is there anyway to improve the performance?

I tried the new c++11 move(), but seems even worse, I must be doing wrong with move(), but I don't know how.

#include <iostream>
#include <string>
#include <ctime>
using namespace std;
struct buffer {
string data;

[Code] .....

View 4 Replies View Related

C# :: How To Avoid Duplicate Updates In SQL Query

Jun 13, 2014

I have table called leavetable where in i have the fields eid, lfrom,lto, reason,status an employee will insert these fields in the leave form except status, status will be updated by admin but there is no unique field in the table so when the admin updates the status as cancel for an id emp001 so whereever this id is present in the table its getting updated to cancel even though it is approved previously.. How to avoid this duplication ?

SqlConnection con = new SqlConnection(Connectionstring);
con.Open();
string sql = "update leavetable set status = '"+status+"' where eid = '"+textBox1.Text+"' and noofdays = '"+textBox5.Text+"'";

[Code] .....

View 7 Replies View Related

Visual C++ :: How To Avoid Memory Fragments

Mar 4, 2015

I am developing a Visual C++ application. There is an object called CMyObject, as follows:

typedef CMap<UINT, UINT, void *, void*> CMyMap;
class CMyObject {
public:
CMyMap *m_pMyMap;
"Some other member variables"
}

Some instances of CMyObject contains a map, some not. Therefore, to save memory, I define a pointer m_pMyMap and create a new CMap object only if the instance contains a map.

When I test my app, with the increase of the CMyObject instance, the number of memory blocks allocated and deallocated is also increasing. There are a lot of fragments during this period. To prevent this, I try to override the new/delete operator for CMyObject and CMyMap. But still find many fragments. So I try to trace into the MFC source codes for CMap. I find CMap is just using an internal buffer to store the hash table(m_pHashTable), as follows:

m_pHashTable = new CAssoc* [nHashSize];

And for each hash entry, it uses:

P = (CPlex *)new BYTE[sizeof(CPlex) + nMax *cbElement];

To allocate the spaces.

I believe these two may be the reason of the memory fragments and want to eliminate them. However, is there a way to override the new/delete operator for codes such as:

new CAssoc* [nHashSize]
and
(CPlex *)new BYTE[sizeof(CPlex) + nMax *cbElement]

So that the spaces will be allocated from my own memory manager instead of from the default heap?

View 1 Replies View Related

C++ :: Only The Last Element Of Array Works

Oct 5, 2014

My code has been acting odd. I made a function that layers my art resources but only the last tile of my art resource acts the way it should. My character goes behind and in front of the last tile and gets printed correctly. Strangely its really exclusive to the last tiles I print. What I need is to figure out in step by step order what going on with my code sample below and be able to layer the resources.

Here is a small sample of my main function. This is how I do my rendering.

Code:
Int main (int arc, char* args[]) {
//Move class
Move character;
//Class Tile & Side Tile
Tile *tiles [TOTAL_TILES];

[Code] ......

View 14 Replies View Related

C :: Comparing Each Element Of One Array With Another

Mar 6, 2015

how to compare each element of array with another,defined ? I have tried this,but didn't work.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bill()

[Code].....

View 3 Replies View Related

C :: Remove Array Element?

Jan 27, 2013

I want to a C program to delete an element from an array which can use both index method & character method
For example

input : "1 222 333 4444"
output:"1 22 333 4444"
if either index = "4" or character ="2" is entered

It should able to read in/accept any input array of varying length, and the position/index or element in array to be deleted...

View 6 Replies View Related

C++ :: How To Delete Element From Array

Jan 9, 2015

how to delete an element(s) from an array; suppose I have an array x[10] = {1,2,3,4,5,6,7,8,9,10}, and I want to delete array{5} so that the values of the array become {1,2,3,4,5,7,8,9,10}; how do I go about this? This is not the same as setting the value of array{5} to null; but completely eliminating it so that it does not get printed alongside the other elements of the screen.

View 3 Replies View Related

C++ :: How To Find First Element In Array

Oct 7, 2013

Lets assume, I use array of 100 the i input 50 element and now i want to find which one is first in array... without using pointer ...

View 2 Replies View Related

C++ :: Deleting Element Of Array

May 6, 2014

This is a program to get an 10 characters including a-z, A-Z, and _ .

Then this program will cout the text with out _ !

Example:
cin >> sd_fd_e_dd
cout << sdfdedd
# include <iostream>
#inclued<vector>
using namespace std;
char a[10],m;

[Code] ....

View 1 Replies View Related

C++ :: Pointer To Array Element?

May 13, 2013

How would I go about having a pointer to an array element specificity a character in a c-string.Every thing I try will not even build.An array is already a pointer to the first location of the array right?

char *pHead;
char *pTail;
pHead = sentence[0]; <=== This wont build
pHead = &sentence[0];
pHead = sentence[0]*;
*pHead = sentence[0]; <===== this builds but is not storing anything

View 5 Replies View Related

C/C++ :: Finding Element Of Array

Feb 17, 2014

"Write a program in C that finds the element of an array which all the previous are smaller and all the next are bigger than that.If there are more than one print the biggest. If there isn't any print "ERROR" .

EXAMPLE

If the array has 10 elements like this : {2,3,4,6,5,7,8,10,9,8}

The correct answer is 7 , because 2,3,4,6,5 are smaller than 7 and 8,10,9,8 are bigger than 7.

I am working on it for 2 weeks and I can't find a working solution />/>/>

There is the first try :

#include <stdio.h>
#include <stdbool.h>
int s_data[10]={2,3,4,6,5,7,8,10,9,8};
int main() {
int result,i,j,s_len ,tmp1,tmp;
bool GT_PREV,ST_NEXT;

[Code] .....

View 11 Replies View Related

C++ :: How To Avoid Using Backspace Character With Push Back

Mar 28, 2014

How to avoid using Backspace character with push_back? I'm making a software as an ATM, so when the user try to enter the password the user only sees *******, but when trying to delete it doesn't delete a character. It just adds a new one. Here's my code:

string password2 = "";
cout << "PASSWORD: ";
ch = _getch();
while(ch != 13) //character 13 is enter {
password2.push_back(ch);
cout << '*';
ch = _getch();
}

And also, I try to use pop_back(); but it doesn't work either.

View 6 Replies View Related

C++ :: Cout Total Element In Array

Feb 28, 2013

I am beginner in C++ programming. I having a problem to show the output of all the element i store at a array called total_price. the output become unreadable.

#include <iostream>
using namespace std;
int main () {
float price[1]={0};
int qty;

[Code] ....

View 6 Replies View Related

C++ :: Remove Element From Array By Using Function?

Jun 2, 2013

how i can remove element from array by using function?

View 3 Replies View Related

C++ :: Allocating Number Of Element On Array

Mar 7, 2013

#include <iostream>
using namespace std;
int main() {
int elm = 0;
int size = 0;
int *array;

[Code] ....

View 6 Replies View Related

C++ :: Inserting New Element Into String Array?

Nov 19, 2013

I have the structure defined in the code below and need to insert a new string into the middle of the string array. I know there is a way to insert a new element with a vector. How to do this. I have tried several variations similar to uniqueList.word.insert(400,"disisdabomb"); but with no luck.

const int maxWordCount=1500;
struct wordCountList
{
string word[maxWordCount];
int count[maxWordCount];
};
wordCountList uniqueList;

View 2 Replies View Related







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