I'm trying to get better at using pointers, and not using array notation. So I have a function to read user input and return a pointer to that array. I can do it like this and it seems to work ok:
float *GetValues(float *p, size_t n)
{
float input;
int i = 0;
if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) {
cout << "Not enough memory\n";
exit(EXIT_FAILURE);
}
cout << "Enter " << n << " float values separated by whitespace: \n";
while (scanf("%f", &input) == 1) {
p[i] = input;
i++;
cout << *p;
}
return p;
}
But then if I do this:
float *GetValues(float *p, size_t n)
{
float *newPtr;
float input;
if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) {
cout << "Not enough memory\n";
exit(EXIT_FAILURE);
}
cout << "Enter " << n << " float values separated by whitespace: \n";
while (scanf("%f", &input) == 1) {
*newPtr++ = input;
}
return newPtr;
}
I get just 0s entered into p. Why is that?
Also, do I have to allocate memory here for an array of size n? I first tried it with the method above using pointers and not allocating memory but just set p = to input and I was getting garbage values. Thanks!
Edited: Sorry, I allocated a new ptr and was returning the wrong one like you said. I was just trying to input the numbers into my pointer and display it back on the screen to myself and wasn't paying attention to the return type and I was getting an output of 0 when I would cout << *newPtr.
Seeing as this is C++, have you considered:
void GetValues(std::vector<float> &values)
{
float input;
while (scanf("%f", &input) == 1) {
values.push_back(input);
}
}
And there's something called iostream classes that you can use instead of scanf. You should check it out!
A few tips:
The first GetValues allocates newPtr (which is not declared within the function, a global variable?) but then does nothing with it. There are two possible ways that your function could work with regards to memory storage:
The function gets a pointer to valid memory for an array of size large enough. In that case the signature of the function ought to be
float *GetValues(float *array, size_t arraySize)
to more clearly state the nature of the arguments. You need not to allocate anything inside the function.
The function should allocate the needed memory itself and let the caller free the memory some time later. In that case you MUST have some kind of hint in the name of the function that it allocates memory. Otherwise you are in for a disaster in terms of maintaining this code because it will be extremely easy to make mistakes in freeing memory. You will not need to pass an array pointer to the function, but if you do it needs to be a double pointer for it to be of any meaning (C and C++ passes arguments by value so it is not possible to change a pointer which is passed as an argument)
float *GetValuesAndAllocateMemmory(size_t n)
float *GetValuesAndAllocateMemmory(float **array_pp, size_t n)
The loop in the second GetValues should be
float *start_p = p;
...
while (scanf("%f", &input) == 1) {
*p++ = input;
cout << *start_p;
}
return start_p;
in order to be identical to the first GetValues.
Here there is upper limit of number of float values is n but your code does not check this
cout << "Enter " << n << " float values separated by whitespace: \n";
while (scanf("%f", &input) == 1) {
and will crash if more than n floats are entered. Never trust user input data, even when it comes from yourself. Always verify. Search for the term "input validation" for more information about this.
cout << "Enter " << n << " float values separated by whitespace: \n";
entered_values = 0;
while (entered_values < n && scanf("%f", &input) == 1) {
entered_values++;
Um, p = newPtr;?
You don't even store any values in p. And you are returning the pointer that you pass as argument, which is not necessary. A better version would be:
long GetValues(float *p, size_t max)
{
float input;
long i;
cout << "Enter at the most " << max << " float values separated by whitespace: \n";
for(i = 0; i < max && scanf("%f", &input) == 1; ++i)
{
p[i] = input;
}
// Return the number of parsed numbers
return i;
}
Related
This is my code in C++. When I am taking input {11 10 5 6 7} in the array, Every time it's giving output smallest as 0. But giving correct output to the other inputs.
#include<bits/stdc++.h>
using namespace std;
int main() {
int a[100000];
int large, small;
int n;
cin >> n;
for (int j = 0; j < n; j++) {
cin >> a[j];
}
for (int j = 0; j < n; j++) {
cout << a[j] << " ";
}
large = small = a[0];
for (int i = 1; i <= n; i++) {
if (a[i] < small) {
small = a[i];
}
if (a[i] > large) {
large = a[i];
}
}
cout << "Smallest is " << small << endl;
cout << "Largest is " << large << endl;
}
You have three loops, two of them run while i<n and the third one while i<=n. And then it uses a[n] item. How do you think what the value of a[n] is?
You were asking:
What is wrong in my code
Let me first answer this question and then we will refactor and optimize it.
So, let's list up the findings
#include<bits/stdc++.h> is non-compliant C++ code. It should never be used. It will run only on selected compilers
using namespace std; should not be used. Always use fully qualified names
C-Style arrays, like a[100000] should not be used in C++. Always use dedicated C++ containers like std::array or std::vector or others. In your case, the size of the array is determined at runtime. So, std::vector must be used
the “100000” is a magic number. Why 100000? Why not 500?. And what happens if the user enters 200000 as array size. Then the program will most likely crash
Use always meaningful variable names. Something like “n” will not be understood. The variable “arraySize” would be understood.
Variables shall always be initialized. And only defined, where they are used and not in the beginning of the program
Input should be verified (cin >> n). What will happen, if the user enters ‘x’ and not ‘5’. Remember, you do not initialize variable n.
large and small must be initialized with the smallest / largest value that the relevant data type can hold. Otherwise the result will always be wrong
array indices start with 0 and not with 1. So, your for loop will fail. The result will be wrong. And you used <=n. So, you will access an out of bounds value.
No need to use endl with cout. ‘\n’ will be sufficient
Then, from the design point of view. You do not need an array at all. You can make all checks immediately, directly after reading the next value.
Anyway, let us make the first step of refactoring. And ths adopted to your programming style. We will
still use C-Style arrays and dynamically allocate the memory
even use raw pointers for owned memory and new. Please note. This should not be done!
correct the bugs
use INT_MIN and INT_MAX
use meaningful variable names and comments
Please see the first refactoring step:
#include <iostream>
#include <climits>
int main() {
// Get the array size from the user and validate the input
unsigned int arraySize{};
if ((std::cin >> arraySize) and (arraySize > 0u)) {
// Now, allocate the memory for the array
int* const array = new int[arraySize]();
// Read all values from user into the just allocated array
for (unsigned int index = 0; index < arraySize; ++index) {
// Read value and check, if OK. If not, value will be 0
if (not (std::cin >> array[index])) std::cerr << "\nError: Wrong value\n";
}
// Now we set up the result values, always with the opposite maximum/minimum
int maxValueInArray = INT_MIN;
int minValueInArray = INT_MAX;
// Iterate over all values and check for min and maximum
for (unsigned int index = 0; index < arraySize; ++index) {
// Compare and assign potential new values
if (array[index] < minValueInArray) minValueInArray = array[index];
if (array[index] > maxValueInArray) maxValueInArray = array[index];
}
// Free the allocated memory. We do not need it any longer
delete [] array;
// Show result to user
std::cout << "Smallest is " << minValueInArray << '\n';
std::cout << "Largest is " << maxValueInArray << '\n';
}
else std::cerr << "\nError while reading array size\n\n";
}
So, next, let’s go a little bit more into the direction C++.
We will get rid of raw pointers, new and will use the correct limit values.
#include <iostream>
#include <limits>
#include <memory>
int main() {
// Get the array size from the user and validate the input
unsigned int arraySize{};
if ((std::cin >> arraySize) and (arraySize > 0u)) {
// Now, allocate the memory for the array
std::unique_ptr<int[]> array = std::make_unique<int[]>(arraySize);
// Read all values from user into the just allocated array
for (unsigned int index = 0; index < arraySize; ++index) {
// Read value and check, if OK. If not, value will be 0
if (not (std::cin >> array[index])) std::cerr << "\nError: Wrong value\n";
}
// Now we set up the result values, always with the opposite maximum/minimum
int maxValueInArray = std::numeric_limits<int>::min();
int minValueInArray = std::numeric_limits<int>::max();
// Iterate over all values and check for min and maximum
for (unsigned int index = 0; index < arraySize; ++index) {
// Compare and assign potential new values
if (array[index] < minValueInArray) minValueInArray = array[index];
if (array[index] > maxValueInArray) maxValueInArray = array[index];
}
// Show result to user
std::cout << "Smallest is " << minValueInArray << '\n';
std::cout << "Largest is " << maxValueInArray << '\n';
}
else std::cerr << "\nError while reading array size\n\n";
}
A little bit better. Now we get rid of the whole manual memory allocation and use a std::vector, which is by far better. And we will use range based for loops, which will make our life simpler:
#include <iostream>
#include <limits>
#include <vector>
int main() {
// Get the array size from the user and validate the input
unsigned int arraySize{};
if ((std::cin >> arraySize) and (arraySize > 0u)) {
// Now, allocate the memory for the array
std::vector<int> data(arraySize, 0);
// Read all values from user into the just allocated array
for (int& value : data) {
// Read value and check, if OK. If not, value will be 0
if (not (std::cin >> value)) std::cerr << "\nError: Wrong value\n";
}
// Now we set up the result values, always with the opposite maximum/minimum
int maxValueInArray = std::numeric_limits<int>::min();
int minValueInArray = std::numeric_limits<int>::max();
// Iterate over all values and check for min and maximum
for (const int& value : data) {
// Compare and assign potential new values
if (value < minValueInArray) minValueInArray = value;
if (value > maxValueInArray) maxValueInArray = value;
}
// Show result to user
std::cout << "Smallest is " << minValueInArray << '\n';
std::cout << "Largest is " << maxValueInArray << '\n';
}
else std::cerr << "\nError while reading array size\n\n";
}
And last but not least, we will get of the whole array/vector stuff. It is not needed.
#include <iostream>
#include <limits>
int main() {
// Get the number of values to check from the user and validate the input
unsigned int numberOfValues{};
if ((std::cin >> numberOfValues) and (numberOfValues > 0u)) {
// Now we set up the result values, always with the opposite maximum/minimum
int maxValueInArray = std::numeric_limits<int>::min();
int minValueInArray = std::numeric_limits<int>::max();
// Read all values from user and check them immediately
for (unsigned int i{}; i<numberOfValues; ++i) {
// Read value and check, if OK. If not, value will be 0
int value{};
if (not (std::cin >> value)) std::cerr << "\nError: Wrong value\n";
// Compare and assign potential new values
if (value < minValueInArray) minValueInArray = value;
if (value > maxValueInArray) maxValueInArray = value;
}
// Show result to user
std::cout << "Smallest is " << minValueInArray << '\n';
std::cout << "Largest is " << maxValueInArray << '\n';
}
else std::cerr << "\nError while reading number of values\n\n";
}
On the third loop it will be
for(int i=0;i<n;i++)
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
void getinput (string &first,string &second);
void lengthcheck (string first, string second);
//int anagramcheck (string word);
int* lettercounter (string input);
int main()
{
std::string a;
std::string b;
getinput(a,b);
lengthcheck (a,b);
lettercounter(a);
lettercounter(b);
int* one = lettercounter(a);
int* two = lettercounter(b);
if (one == two)
cout << "You Have Entered An Anagram" << endl;
else
cout << "You Have Not Entered An Anagram" << endl;
}
void getinput (string &first, string &second) {
cout << "Enter First Input: ";
getline(cin, first, '\n');
cout << "Enter Second Input: ";
getline(cin, second, '\n');
cout << "You Entered " << first << " and " << second <<endl;
}
void lengthcheck(string first, string second){
int lengtha = first.length();
int lengthb = second.length();
if ((lengthb > 60) || (lengtha > 60)) {
cout << "Input Is Invalid" << endl;
} else if (lengtha !=lengthb) {
cout << "Input is not an anagram" << endl;
} else {
cout << "Input is Valid" << endl;
}
}
int* lettercounter(string input)
{
static int freq[26] = {0};
int length = input.length();
for (int i=0; i<26; i++) {
freq[i]=0;
}
for (int i=0; i <length; i++) {
if(input[i]>='a' && input[i]<='z')
{
freq[input[i] - 97]++;
}
else if(input[i]>='A' && input[i]<='Z')
{
freq[input[i] - 65]++;
}
}
for(int i=0; i<26; i++) {
/* If current character exists in given string */
if(freq[i] != 0)
{
printf("'%c' = %d\n", (i + 97), freq[i]);
}
return freq;
}
}
I am having trouble returning the array named freq from the user definied function called lettercount. Can someone give me a hint? I need the lettercount to return an array. I need to call the function lettercount twice so i can compare the results of each array to determine if the two inputs are anagrams. I am not sure if the function is returning an actual value to the main.
First of all, freq shouldn't be static. By making it static, you would be accessing the same array everytime. For what you want to do, you don't want to always access the same memory.
In second place, you cannot just return a pointer to memory that has not being allocated dynamically or that isn't static. When you get out of scope (i.e. you return from the function lettercounter back to main), the memory that was occupied by the array will be freed. So, you would be returning a pointer to memory that is no longer reserved, resulting in undefined behavior.
If you really need to work with raw pointers, then each time you enter lettercounter, you would need to allocate memory for the array dynamically like this: int * freq = new int[26];. This will reserve memory for an array of size 26. Then, when you return freq, the memory will still be allocated. However, don't forget that the memory allocated with new doesn't delete itself. You have to clean your mess. In this case, at the end of main you would call delete[] one; and delete[] two;.
int* lettercounter(string input)
{
int * freq = new int[26];
.
.
.
return freq;
}
int main()
{
.
.
int* one = lettercounter(a);
int* two = lettercounter(b);
.
.
delete[] one;
delete[] two;
}
In any case, I'd recommend you to learn to use smart pointers and about standard containers (like a vector). These operations would be much simpler.
There is a problem with the code and i could not find it.
i was asked to write a money struct and use functions to manipulate it.
but the code did not work for any function. i tried couting
the array of structers and it came out nicely, for any missing info
please leave a comment and i'll reply shortly.
Money.txt
2
12 20
13 40
#include <iostream>
#include <fstream>
using namespace std;
struct Money { //declaring structure
int dollars;
int cents;
};
Money addMoney(Money *p[], int n) { //adds money data
Money cash{ 0,0 };
int i;
for (int j = 0; j < n; j++) {
cash.dollars = cash.dollars + p[j]->dollars;
cash.cents = cash.cents + p[j]->cents;
}
if (cash.cents >= 100) //100cents = 1 dollar
{
i = (cash.cents) / 100;
cash.dollars = cash.dollars + i;
i = (cash.cents) % 100;
cash.cents = i;
}
return cash;
}
void printMoney(Money *p[], int n) { //printing money data
for (int i = 0; i < n; i++) {
cout << "Dollars: " << p[i]->dollars << endl;
cout << "Cents: " << p[i]->cents << endl;
}
}
Money maxMoney(Money *p[], int n) {
Money cash;
cash.dollars = p[0]->dollars;
cash.cents = p[0]->cents;
for (int i = 0; i < n; i++)
{
if ((p[i]->dollars)>=(cash.dollars))
if ((p[i]->cents)>(cash.cents))
{
cash.dollars = p[i]->dollars;
cash.cents = p[i]->cents;
}
}
return cash;
}
void main() {
Money cash;
ifstream mycin("money.txt");
if (mycin.fail())
cout << "Enable to open file";
int x;
mycin >> x;
Money *arr = new Money[x];
for (int i = 0; i < x; i++)
{
mycin >> arr[i].dollars;
mycin >> arr[i].cents;
}
cout << "The values in money.txt are: ";
printMoney(&arr, x);
cash = addMoney(&arr, x);
cout << "These values added are :";
cout << cash.dollars << " Dollars and " << cash.cents << " cents" << endl;
cash = maxMoney(&arr, x);
cout << "Maximum value is :";
cout << cash.dollars << " Dollars and " << cash.cents << " cents" << endl;
}
These functions appear to accept an array of pointers to Money, but you're trying to use them with an array of Money.
I suggest you play with arrays of pointers to simpler types (like int) until you're comfortable with the concept, before you attempt it with Money.
This sounds a lot like homework so I'm not posting a full solution, but I will explain what appears to be the misunderstanding and give you some pointers.
First you declare your data structure as an array of Money structures, e.g. a continuous series of blocks of memory containing the Money struct, the first of which is pointed to by "arr" in your main program.
But then, in the rest of the program (functions) you seem to expect the data structure being used to be an array of Money pointers. See the difference? They're not the same and this will not work as is. You have to be consistent.
Either you're dealing with an array of structs, in which case you pass effectively a single, simple Money* to your functions everywhere (and you dereference with . not ->)
Or you're dealing with an array of pointers, in which case you pass effectively a pointer to a (Money pointer) and you dereference with -> as you've done. But then you also have to allocate each Money struct individually when you're reading them in in the main program. That is to say, allocating memory for the array of pointers does not automatically allocate memory for each Money pointer reference in the array of pointers and so you need to do this for each entry you're reading in.
So, as you should hopefully now realise, there's multiple ways to fix your program.
As per your later comment, given that the function signatures need to stay as-is, I would suggest you work with an array of Money pointers.
Money** arr = new Money*[x]
Then you need to add a line to your loop during reading, to actually make each Money * point to a Money struct:
for (int i = 0; i < x; i++)
{
arr[i] = new Money
...
Finally then, because "arr" is now a pointer to a pointer to Money, you can directly pass it to your functions, so calling them are just for example:
printMoney(arr, x);
I'm having trouble with a program I'm doing in class and even the teacher can't find the problem. We're doing a program that ask the user to enter double then when he stop, it scan the array and separate the positive and negative to put them in different arrays.
We notice that when we use float the program work for more numbers but still bug if we enter too much and if we use double it bug after only a few numbers. By bug I mean, the program do well but when it display the result there is some weird numbers in the array. Here is the code using double:
#include <iostream>
using namespace std;
void filling(double *, int &);
void sortPositiveNegative(double *, double *, double *, int, int &, int &);
void display(const double *, int);
int main () {
double * vecteur = new double;
double * positive = new double;
double * negative = new double;
int counter = 0, counterPos = 0, counterNeg = 0;
cout << "Filling of the real number vector " << endl;
filling(vecteur, counter);
cout << endl << "Display of the real number vector " << endl;
display(vecteur, counter);
cout << endl << "Sort of the positive and negative in the real number vector: " << endl;
sortPositiveNegative(vecteur, positive, negative, counter, counterPos, counterNeg);
cout << endl << "Display of the positive real number : " << endl;
display(positive, counterPos);
cout << endl << "Display of the negative real number : " << endl;
display(negative, counterNeg);
system("PAUSE");
return 0;
}
void filling (double *vecteur, int &counter)
{
bool validation;
char choice = 'Y';
do
{
do
{
validation = true;
cout << "Please enter the value of case " << counter+1 << ": ";
cin >> vecteur[counter];
if(cin.fail())
{
cerr << "The number entered is not valid." << endl;
cin.clear();
validation = false;
}
while(cin.get() != '\n'){}
}while(!validation);
counter++;
do
{
validation = true;
cout <<"Do you wish to continue? (Y/N): ";
cin >> choice;
if(toupper(choice) != 'Y' && toupper(choice) != 'N')
{
cerr << "We don't understand your choice, please try again." << endl;
cin.clear();
validation = false;
}
while(cin.get() != '\n'){}
}while(!validation);
}
while(toupper(choice)=='Y');
}
void sortPositiveNegative(double *vecteur, double *positive, double *negative, int counter, int &counterPos, int &counterNeg)
{
int i = 0;
for(i; i<counter;i++)
{
if(vecteur[i] >= 0)
positive[counterPos++] = vecteur[i];
else
negative[counterNeg++] = vecteur[i];
}
}
void display (const double *vecteur, int counter)
{
for(int i = 0; i<counter;i++)
cout << vecteur[i]<<endl;
cout << endl;
}
My teacher think it might be a memory problem but we have no idea why it's doing that.
Thanks in advance.
There is definitely a memory problem, and I don't see how using float would fix it. For example, the below line of code allocated only one double and not an array of doubles:
double * vecteur = new double;
Then, you use this vecteur as if it was an array of N elements. That triggers undefined behavior.
To fix it, you would have to allocate an array of as much values as you need. For example, let's say you need 10, then you allocate 10 like this:
double * vecteur = new double[10];
However, given that you don't know the number of elements in advance, you would need to extend the array every time you want to add an element. If you were writing this in C, I would have recommended you to use realloc(). But given that you use C++, just stick with std::vector<double>, it will manage memory automatically. For example:
#include <vector>
int main()
{
std::vector<double> vecteur; // Use vector to store array of doubles.
// Add as many elements as you want.
// Vector will resize itself if/when needed.
vecteur.push_back(.1);
vecteur.push_back(.2);
vecteur.push_back(.3);
vecteur.push_back(.4);
vecteur.push_back(.5);
}
Hope it helps. Good Luck!
double * vecteur = new double;
You allocate space for one doubile
filling(vecteur, counter);
Pass it to filling
cin >> vecteur[counter];
And fill untill user presses Y going outside of array with one element you have allocated memory for.
double vs float doesn't matter much. float is simply smaller and thus will be corrupting memory slower. But it is still corrupting memory starting from vecteur[1]
I'd suggest you to use std::vector<dobule> instead of plain pointers, and filling it with push_back
double * vecteur = new double;
double * positive = new double;
double * negative = new double;
Here you allocate exactly ONE double each time. The first item you store in your "array" is fine, but anything else after that is undefined behaviour.
The fix is to actually allocate as many items as you need:
double * vecteur = new double[MAXIMUM8NUMBER_OF_ITEMS];
double * positive = new double[MAXIMUM8NUMBER_OF_ITEMS];
double * negative = new double[MAXIMUM8NUMBER_OF_ITEMS];
Or better yet, use a standard container like std::vector.
I am having a problem with the program I am trying to code. It's just a Windows console program and I am very new to C++. It's only my 4th program.
The problem I am having is that when I run my program I have no errors but a lot of warnings that say "comparison with string literal results in unspecified behaviour" in the lines that I will highlight below.
When the program runs instead of adding the numbers I want it to it just gives me a random huge number no matter what I put in for my inputs.
Here is the code:
#include <iostream>
using namespace std;
int main()
{
int hold;
int i;
int n;
i = 6;
int result;
int * price;
char items[100][100];
if (items == 0)
cout << "No items can be stored";
else
{
for (n=0; n<i; n++)
{
cout << "Item#" << n << ": ";
cin >> items[n];
}
cout << "\nYou Entered: \n";
for (n=0; n<i; n++)
cout << items[n] << ", ";
}
for (n=0; n<i; n++)
{
if (items[n] == "ab"){
price[n] = 2650;
}
else if (items[n] == "ae"){
price[n] = 1925;
}
else if (items[n] == "ie"){
price[n] = 3850;
}
else if (items[n] == "bt"){
price[n] = 3000;
}
else if (items[n] == "pd"){
price[n] = 2850;
}
else if (items[n] == "ga"){
price[n] = 2600;
}
}
for (n=0; n<i; n++)
{
result = result + price[n];
}
cout << "\nTotal gold for this build: " << result;
cin >> hold;
return 0;
}
Any help is appreciated. There is probably something big that I've done wrong. The names in the if statements are all currently placeholders and I'll be adding a lot more if statements when I can get it to work with the bare 6 which is what it needs to work.
In C++ == only implemented internally for primitive types and array is not a primitive type, so comparing char[100] and string literal will only compare them as 2 char* or better to say as 2 pointers and since this 2 pointers can't be equal then items[n] == "ae" can never be true, instead of this you should either use std::string to hold string as:
std::string items[100];
// initialize items
if( items[n] == "ae" ) ...
or you should use strcmp to compare strings, but remeber strcmp return 0 for equal strings, so your code will be as:
char items[100][100];
// initialize items
if( strcmp(items[n], "ae") == 0 ) ...
And one extra note is if (items == 0) is useless, since items allocated on stack and not in the heap!
First, int * price; is a dangling pointer - you never initialize it. You have to do:
int * price = new int[i];
Second, usually, i denotes an iterator index so I suggest you stick with that - so
for (i=0; i<n; i++) //some more refactoring needed
Third, you need to compare char arrays using strncmp in your case.
Fourth and most important - use std::string and std::vector instead. This is C++, not C.
Just a little thing that got me stumbling for a bit, is the difference between single and double quotes, see: Single quotes vs. double quotes in C or C++
I was comparing the first character of a string with double quotes and not single quotes - which resulted in above's error message.
You're comparing pointers, not the actual strings. Use C++ string class instead of char* (or check how C strings work).