C++ Bubble sort - c++

I may just be having a brain fart, upon doing the bubble sort algorithm, of course, I encountered a problem. Any help will be golden. The problem is to do with the "float numbers[n];" and the "std:string c;".
int n = 0;
cout << "Enter value of N: ";
cin >> n;
float numbers[n];
cout << "You will enter " << n << " numbers." << endl;
for (int i = 0; i < n; i++) {
std:string c;
cin >> c;
numbers[i] = atof(c.c_str());
}

It is not exactly clear to me (or us) what your problem exactly is. But in any case, std:string has to be replaced by std::string. Double colon sign, indicating that string lies in the std namespace.
Besides, it is much more idiomatic, and more portable and less error-prone, if you replace the C-style array by a proper std::vector object.
This code compiles OK:
#include <string>
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int n = 0;
cout << "Enter value of N: ";
cin >> n;
std::vector<float> numbers(n);
cout << "You will enter " << n << " numbers." << endl;
for (int i = 0; i < n; i++) {
std::string c;
cin >> c;
numbers[i] = stof(c);
}
// etc...
return EXIT_SUCCESS;
}

Related

I got infinite loop while practicing array in C++ to find reversed number

Hye, Im a beginner trying to learn C++ language. This is my code that I tried to find reverse input numbers using array. Can help me point my mistakes since I always got infinite loop.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int ARRAY_SIZE=50;
int size[ARRAY_SIZE];
unsigned short int i;
cout << "You may enter up to 50 integers:\n";
cout << "\nHow many would you like to enter? ";
cin >> size[ARRAY_SIZE];
cout << "Enter your number: \n";
for (int i = 0; i < ARRAY_SIZE; i++)
{
cin >> size[i];
}
cout << "\nYour numbers reversed are:\n";
for (i = size[ARRAY_SIZE] - 1; i >= 0; i++)
cout << " size[i]" << " ";
}
Your infinite loop is because i is unsigned, so i >= 0 is always true.
Here's a C++-ified version:
#include <iostream>
#include <vector>
int main() {
std::cout << "You may enter up to 50 integers:\n";
std::cout << "\nHow many would you like to enter? ";
int count;
std::cin >> count;
// Use a std::vector which can be extended easily
std::vector<int> numbers;
for (int i = 0; i < count; ++i) {
std::cout << "Enter your number: \n";
int v;
std::cin >> v;
// Add this number to the list
numbers.push_back(v);
}
std::cout << "\nYour numbers reversed are:\n";
// Use a reverse iterator to iterate through the list backwards
for (auto i = numbers.rbegin(); i != numbers.rend(); ++i) {
// An iterator needs to be de-referenced with * to yield the value
std::cout << *i << " ";
}
std::cout << std::endl;
return 0;
}
There's many problems in your original code, but the clincher is this:
for (i = size[ARRAY_SIZE] - 1; i >= 0; i++)
cout << " size[i]" << " ";
}
Since you keep adding to i through each cycle you'll never go below zero, especially not for an unsigned short int. This should be:
for (int i = count - 1; i > 0; --i) {
std::cout << numbers[i];
}
Presuming you have a thing called numbers instead of the bizarrely named size and the array size is count, not i, as i is generally reserved for iterators and loop indexes.

Half of array cut of in C++

I'm currently self teaching C++, and have a problem. the purpose of this code is to print asterisks in a pyramiding fashion, with the example being if the input (variable int n) is 5, it should print like this:
*
**
***
****
*****
Here's the code:
#include <iostream>
using namespace std;
int main()
{
int i, n;
cout << "What is your number?" << endl;
cin >> n;
cout << "n is: " << n << endl;
int arr[n];
int z = n;
while(z > 0){
arr[z] = 0;
z--;
}
z = n;
for(int y = 0; y<=z; y++){
arr[y] = z;
cout << "Y is: " << y << endl;
cout << "Arr[Y] is: " << arr[y] << endl;
cout << "z is: " << z << endl;
z--;
}
while(n > 0){
int x = arr[n];
while(x > 0){
cout << "*";
x--;
}
cout << endl;
n--;
}
}
but the first half (rounded up) will always be blank on printing. I don't know how to debug in CodeBlocks yet, so I can't tell you what's hiding in the memory to solve this myself
Although this does not fix your code, you stated that you are currently learning C++, so I think providing a better solution is even much better.
Here is another simpler way to achieve your goal:
#include <iostream>
using namespace std;
int main()
{
unsigned int i, n;
cout << "What is your number?" << endl;
cin >> n;
for (auto i = 0; i < n; i++)
{
for (auto j = 0; j <= i; j++)
{
cout << "*";
}
cout << endl;
}
}
Remember, trying to implement an easy, simple and efficient way is always better for performance, readability and debugging. Good luck in your learning and happy coding!
The most easiest way to do this
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << string(i, '*') << endl;
}
return 0;
}

Not reading C++ input

I have only a little experience in C++. I'm trying to write a program to print each element of the 'sales' array:
#include <iostream>
#include <iomanip>
using namespace std;
void printArray(int, int);
int main()
{
char chips[5][50] = {"mild", "medium", "sweet", "hot", "zesty"};
int sales[5] = {0};
int tempSales, counter;
const int i = 5;
for (counter = 0; counter < i; counter++)
{
cout << "Please enter in the sales for " << chips[counter] << ": ";
cin >> tempSales;
tempSales >> sales[counter][5];
}
cout << "{";
for (int counter = 0; counter < i; counter++)
{
cout << chips[counter] << ", ";
}
cout << "}" << endl;
cout << "{";
for (int counter = 0; counter < i; counter++)
{
cout << sales[counter] << ", ";
}
cout << "}" << endl;
return 0;
}
To solve this problem, I need to have the same commands and keywords I still have, and it can't be any advanced or weird syntax. For some reason, my input from the:
cin >> tempSales
Is not functioning. Here are the results:
{mild, medium, sweet, hot, zesty, }
{0,0,0,0,0, }
Whereas I just want to see 1, 2, 3, 4, and 5 for the second array. Why is it only print 0 and not reading my input? Please help!
Like rranjik stated, you shouldn't need a 2D array if you're only listing the number of sales, which it appears you're doing from what you provided, is this not the case?
Is it necessary for you to use the bitshift operator >> for your assignment? For a simple integer assignment it isn't really necessary, and you could do:
int sales[5] = {0}; Change the array to a simple array instead of 2D.
sales[counter] = tempSales; Use standard assignment for the integer on line 19
cout << sales[counter] << ", "; Change your output accordingly.
Hope this helps!
I don't think you need a 2D array for sales. Try cout << sales[counter][5] << ", "; or change int sales[5][6] = {0}; to int sales[5] = {0};. As Luke mentioned, use standard assignment sales[counter] = tempSales;.

c++: how to use char / string 2d array to accept more than 1 sentense from an user?

Below code failed:
#include <iostream>
#include <string>
using namespace std;
int main(){
int a, b;
cout << "how many input you want to give ? ";
cin >> a;
b = a - 1;
string str[b];
for(int i = 0; i <= b; i++){
cout << "Enter a string: ";
getline(cin, str[i]);
}
for(int k = 0; k < a; k++){
cout << "You entered: " << str[k] << endl;
}
return 0;
}
But if I fix the value of 'int a' then the code is running. please help.
Arrays must have a constant size at compile-time so to create an array with dynamic size you can create it on the heap using keyword new and delete[] to free up memory.
Also what is the point in:
cin >> a;
b = a - 1; //?
You can easily do it like this:
int n;
cout << "how many input you want to give ? ";
cin >> n;
string* str = new string[n];
for(int i = 0; i < n; i++){
cout << "Enter a string: ";
getline(cin, str[i]);
}
for(int k = 0; k < n; k++){
cout << "You entered: " << str[k] << endl;
}
Don't forget to clean when you're done:
delete[] str;
You'll want to clear the input buffer, the newline from "How many input you want to give ?" is being fed into the first "Enter a string:".
Add this line after cin>>a;
cin.ignore(INT_MAX, '\n');
Use a vector to store the input.
#include <vector>
#include <string>
#include <iostream>
using names pace std;
int main ()
{
vector <string> input;
string tmp;
while (getline(cin, tmp))
input.push_back(tmp));
for(auto s : input)
cout << s << '\n';
}

Looping and strcpy

#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
cout << "Enter " << n << "names";
for(int i=0; i<n; i++)
{
system("pause>0");
return 0;
}
This is my unfinished code. I'm required to enter a number then it will ask me to enter n names. and after entering the names the program should sort the names alphabetically. How will I do that in Looping? I'm very confused in the looping part. Yeah, I know what I will code when I'm finished in looping. I'm just confused and having problems in this part. Thanks in Advance!
Here's an STL version of what you're trying to do:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <algorithm>
int main() {
std::vector<std::string> names;
int num = 0;
std::cout << "Please enter a number: ";
std::cin >> num;
std::cout << "\n";
std::string name;
for (int i = 0; i < num; ++i) {
std::cout << "Please enter name(" << (i+1) << "): ";
std::cin >> name;
names.push_back(name);
}
//sort the vector:
std::sort(names.begin(), names.end());
std::cout << "The sorted names are: \n";
for (int i=0; i<num; ++i) {
std::cout << names[i] << "\n";
}
return 0;
}
However, this version is a case-sensitive sort, so whether or not that performs to your requirements could be problematic. So, a possible next step to get closer to case-insensitive sorting is to use this bit of code before the vector is sorted:
//transform the vector of strings into lowercase for case-insensitive comparison
for (std::vector<std::string>::iterator it=names.begin(); it != names.end(); ++it) {
name = *it;
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
*it = name;
}
The only caveat with this method is that all your string will be converted to lowercase, though.
REFERENCES:
https://stackoverflow.com/a/688068/866930
How to convert std::string to lower case?