Question:
(C++ Programming) How can i fix my array size?
KIMBERLY C
2009-02-13 00:59:29 UTC
i need some assistance with making the output come out properly.

Write a function called delete_repeats that has a partially filled array of characters as a formal parameter and that deletes all repeated letters from the array. Since a partially filled array requires two arguments, the function will actually have two formal parameters: an array parameter and a formal parameter of type int that gives the number of array positions used. When a letter is deleted the remaining letters are moved forward to fill in the gap. This will create empty positions at the end of the array, so that less of the array is used. Since the formal parameter is a partially filled array, a second formal parameter of type int will tell how many positions are filled. This second formal parameter will be a call-by-reference parameter and will be changed to show how much of the array is used after the repeated letters are deleted.
This program will will ask the user to type in a sentence
and then will delete all repeated characters of the sentence.
The program will then output the new sentence with all repeated
letters deleted

p.s. it only displays 4 letters. why...

//code
#include
#include

using namespace std;

void fill_array(char a[], int size, int& number_used);
//Array a[] is filled with data from the keyboard

void delete_repeats(char a[], int& number_used);
//Function will remove all repeated characters and move the rest of the characters
//foward to fill in the gap.

void output(char a[], int& number_used);
//Outputs the contents of the array and outs the new size of the array

int main()
{
char array[100];
int number_used;

cout << "This program will will ask the user to type in a sentence\n"
<< "and then will delete all repeated characters of the sentence.\n"
<< "The program will then output the new sentence with all repeated\n"
<< "letters deleted\n";

fill_array(array, 100, number_used);
delete_repeats(array, number_used);
output(array, number_used);

system("PAUSE");
return 0;
}



//uses iostream
void fill_array(char a[], int size, int& number_used)
{
char c;
int index = 0;
cout << "Please type in a sentence and then press enter.\n";
cin.get(c);
while (c != '\n' && index < size)
{
index++;
a[index] = c;
cin.get(c);
}
number_used = index;
}

//uses iostream
void delete_repeats(char a[], int& number_used)
{
for (int i = 0; i < number_used; i++)
{
for (int j = i + 1; j < number_used; j++)
{
if (a[i] == a[j])
{
for (int k = j; k < number_used; k++)
a[k] = a[k + 1];
}
}
}
number_used = sizeof a;
}

//uses iostream
void output(char a[], int& number_used)
{
cout << "The new sentence without the repeated letters is:\n";
for (int i = 0; i < number_used; i++)
{
cout << a[i];
}
cout << "\nThe size of the new array is "
<< number_used
<< endl;
}




//output
Please type in a sentence and then press enter.
hello
The new sentence without the repeated letters is:
xhel
The size of the new array is 4
Press any key to continue . . .
Three answers:
justme
2009-02-13 09:19:57 UTC
one thing you are doing wrong is in this bit of code:



while (c != '\n' && index < size)

{

index++;

a[index] = c;

cin.get(c);

}

change it to this:



while (c != '\n' && index < size)

{

a[index] = c;

index++;

cin.get(c);

}





In this routine, change this:



void delete_repeats(char a[], int& number_used)

{

for (int i = 0; i < number_used; i++)

{

for (int j = i + 1; j < number_used; j++)

{

if (a[i] == a[j])

{

for (int k = j; k < number_used; k++)

a[k] = a[k + 1];

}

}

}

number_used = sizeof a;

}



to this:



void delete_repeats(char a[], int& number_used)

{

for (int i = 0; i < number_used; i++)

{

for (int j = i + 1; j < number_used; j++)

{

if (a[i] == a[j])

{

for (int k = j; k < number_used; k++)

a[k] = a[k + 1];

number_used--;

}

}

}

a[number_used] = 0;//NULL the string

}
Germann A
2009-02-13 02:19:32 UTC
1) This is NOT Basic, this is C[++], the function CAN (and should) return the result, there is NO point in defining

void fill_array(char a[], int size, int& number_used);

when it is more logical to define

int fill_array(char a[], int size); so it will return "number_used" value...



2) [I do NOT have C compiler to check this, but AFAIR] the way your functions are defined now you will need to call them passing the ADDRESS of number_used...

fill_array(array, 100, &number_used);

delete_repeats(array, &number_used);

output(array, &number_used);



But, still you will be better off reworking your functions...
?
2016-11-06 02:14:43 UTC
the two sizeof(a) and sizeof(a[0]) are evaluated at convey at the same time time. The compiler that compiles function f has no theory of the honestly length of the array a. So your expectation isn't proper. in case you had pasted the code which you wrote in f, straight away in important, then this technique might behave as you predicted. to remedy the situation, you ought to bypass yet another argument to f as in f(glide* a, size_t length ); subsequently, you will use the runtime variable length somewhat of the convey at the same time time fee. So why do you get a million : because of the fact sizeof(glide) is 4 and sizeof (glide*) is likewise 4.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...