I need to make a program where a user inputs a string of numbers.
It then outputs the numbers grouped so that each grouping, separated by a space, is larger than the last when you put the numbers together.
If the remaining digits together equal a sum smaller than the previous grouping they are omitted.
Ex.:
314159265 would output 3 14 15 92
Here: 3 < 14 <15 < 92 but 65 was omitted because it is less than 92
or
9876543210 would output 9 87 654 3210
Here: 9 < 87 < 654 < 3210
It has to do this 5 times with strings 1-20 characters long.
My code works for shorter strings, ie the ones above, but when they are longer than around 12 characters the end output messes up.
Ex.:
98765432101234567898 outputs 9 87 654 3211 12333 instead of 9 81 654 3210 12345 67898
Here: 9 < 87 < 654 < 3211 < 12333 it should output 9 < 87 < 654 < 3210 < 12345 < 67898
I have no idea why it doesn't work with larger strings and any help would be greatly appreciated.
#include<iostream>
#include<iomanip>
#include<math.h>
#include<string>
#include<stdlib.h>
using namespace std;
void input (string &a,string num[20]){
string numfinal,temp;
cout<<"Enter the string of numbers: ";
getline(cin, a);
int length=a.length();
for(int i=0;i<length;i++){
num[i]=a.substr(i,1);
}
for(int r=0;r<length;r++){
int n=atoi(temp.c_str());
int o=atoi(num[r].c_str());
int p=temp.length();
if((length-r<=p)&&(o<n)){
}
else if((o>n)||(r==0)){
temp=num[r];
numfinal=numfinal+temp+" ";
}
else if((o<n)||(o=n)){
int w=n;
temp=num[r]+num[r+1];
n=atoi(temp.c_str());
if(n<w){
int a=1;
int q=r+2;
while(n<w){
temp=temp+num[q];
n=atoi(temp.c_str());
p++;
a++;
}
numfinal=numfinal+temp+" ";
r=r+a;
}
else{
numfinal=numfinal+temp+" ";
r++;
}
}
}
cout<<numfinal<<endl;
}
int main(){
string a;
string num[20];
for(int r=0;r<5;r++){
input(a,num);
}
return 0;
}
This code works. But do not forget that write a simple code and use the new style of C++ programming.
#include <vector>
using namespace std;
vector<string> Input( )
{
string a;
cin >> a;
vector<string> num;
string current("-1");
string str;
for(auto c : a)
{
str.append(1, c);
if (stoi(str) > stoi(current) )
{
num.push_back(str);
current = str;
cout << str << " ";
str = "";
}
}
cout << endl;
return num;
}
int main() {
for (int i = 0; i<5; i++) {
vector<string> num;
num = Input();
}
return 0;
}
Looks like you are missing a q++ in your while loop:
while(n < w) {
temp = temp + num[q];
n = atoi(temp.c_str());
p++;
a++;
q++;
}
This is why the fourth third digit was being copied over.
The maximal munch algorithm might give you some inspiration on a cleaner way to solve this problem. It is meant for tokenizing but it may help. Good luck!
Related
For example:
First file has these numbers:
10 1 5 -3 -9 -12 34 0 2 -52
Second file:
-52 -9 0 2 10
Third file:
-12 -3 1 5 34
My code:
#include <fstream>
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
void main()
{
srand(time(NULL));
int n = 10;
int *a = new int[n];
ifstream f("numbers.txt");
ofstream g("AscNum1.txt");
ofstream h("AscNum2.txt");
while (f.is_open()) {
for (int i = 0; i < n; i++) {
f >> a[i]; //reading numbers from file
}
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
g << a[i] << ' ';//writing number if its index is even
}
else
h << a[i] << ' '; //writing number if its index is not even
}
h.close();
g.close();
f.close();
}
}
In this code, i sort before "transferring" and "transfer" by even index(Second file contains numbers with even indexes).
How can I "transfer" without using sort? How can I make this by different way?
I want to sort using the "Bubble Sort" algorithm of the 2d array. My array size will be about array[100000][100000]. my input number will be n=100,000.
For now we can use a small size of the array to fix the sorting issue.
I need to sort them in descending order for the first number(first number's line).
If the first number of 2 values are the same, then I have to sort them according to their second number.
Finally I have to output the result into a txt file
Let's' understand using an example. Here, my input looks like this
41 11
34 4
69 4
78 6
62 8
5 5
81 3
5 10
above our input example and we have a couple of inputs. Now I need to sort them descending orders for the first number. But if the first number of 2 values are the same, then sort them according to their second number.
Example output below,
81 3
78 6
69 4
62 8
41 4
34 4
5 10
5 5
If anyone can please help me.
I am a beginner so I am trying to input the file manually to solve this sorting problem. I can solve the sorting problem then I will try to input and out the text.
Something I have tried but not worked. I am still trying to solve it.
#include<bits/stdc++.h>
#include <algorithm>
using namespace std;
int main ()
{
int arr[100][100];
int n,j;
cin >>n;
cout << "Please enter a number: " << endl;
for(int i=0;i<n;i++)
{ for (int j=i; j<n; j++)
{
cin>>arr[i][j];
}
}
cout << "Unsorted array:" << endl;
for (int i=0; i<n; i++)
{
for (int j=i; j<n; j++)
{
cout<<arr[i][j]<<"\t";
}
}
for (int i=0; i<=n; i++)
{
for (int j=i+1; j<=n-1; j++)
{
int temp;
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
return 0;
}
Use a std::vector<std::array<int,2>>for your base container. The dynamic growth capabilities of std::vector solves your stack space issue, and the std::array use gives you tied cell comparison. I.e. you can do this:
std::array<int, 2> ar1{1,2}, ar2{1,3};
if (ar1 < ar2) ...
and it will do the right thing. The result then boils down to effectively this:
#include <iostream>
#include <array>
#include <vector>
#include <utility>
int main()
{
std::vector< std::array<int,2> > v;
std::size_t n;
if (std::cin >> n && n > 0)
{
std::array<int,2> row;
while (n-- && std::cin >> row[0] && std::cin >> row[1])
v.emplace_back(row);
// bubblesort the content
std::size_t len = v.size();
while (len-- > 0)
{
bool swapped = false;
for (std::size_t i=0; i<len; ++i)
{
// std::array support multi-cell comparison.
if (v[i] < v[i+1])
{
// use library swap to swap entire row.
std::swap(v[i], v[i+1]);
swapped = true;
}
}
// early exit if no swaps happened on the last pass
if (!swapped)
break;
}
// report final output.
for (auto const& row : v)
std::cout << row[0] << ' ' << row[1] << '\n';
}
}
Input
8
41 11
34 4
69 4
78 6
62 8
5 5
81 3
5 10
Output
81 3
78 6
69 4
62 8
41 11
34 4
5 10
5 5
Write a program in C++ that takes in an integer in the range 20-98 as input. The output is a countdown starting from the integer, and stopping when both output digits are identical.
Ex: If the input is:
93
the output is:
93 92 91 90 89 88
I'm having trouble with how I would compare both digits to see if they're identical.
You can use / and %:
#include <iostream>
int main() {
int i{};
std::cin >> i;
if (i > 98 || i < 20) {
return -1;
}
while (i / 10 != i % 10) {
std::cout << i-- << ' ';
}
std::cout << i << '\n';
}
#include <iostream>
using namespace std;
int main(void) {
int i;
std::cin >> i;
do { cout <<i<< endl; } while (i--%11);
return 0;
}
Split the number into the digits (LHS and RHS) and then compare if both are same.
To get the LHS use the divide operator (/).
To get the RHS use the mod operator (%)
Example pseudo code:
int iNum = 98;
// iRHS will have 9
int iRHS = iNum % 10;
// iLHS will have 8
int iLHS = iNum/10;
if (iRHS == iLHS) {
// Both digits are same.
}
Hope this helps.
ps: I did not compile the code
I wrote a program where program sorts people by the each person time, and if time is the same, program sorts by the name alphabetically. Everything works fine, just I am getting extra first line full of random symbols. I of course can write if function and say not to show that first line, but is it good thing to do?
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
struct people {
string full_name;
int h;
int min;
int sec;
};
bool comp(const people &p1, const people &p2) {
return (p1.min < p2.min || p1.min == p2.min && p1.sec < p2.sec ||
p1.min == p2.min && p1.sec == p2.sec && p1.full_name < p2.full_name);
}
int main() {
int time;
string fullnamechange[30];
people peo[30];
cin >> time;
for (int i = 0; i < time; i++) {
getline(cin, peo[i].full_name); // taking everything into a string
fullnamechange[i] = peo[i].full_name;
fullnamechange[i].erase(fullnamechange[i].begin(),
fullnamechange[i].end() - 8);
peo[i].h = atoi(fullnamechange[i].c_str());
fullnamechange[i].erase(fullnamechange[i].begin(),
fullnamechange[i].end() -
5); // changing time in string to int
peo[i].min = atoi(fullnamechange[i].c_str());
fullnamechange[i].erase(fullnamechange[i].begin(),
fullnamechange[i].end() - 2);
peo[i].sec = atoi(fullnamechange[i].c_str());
}
for (int i = 0; i < time; i++) { // erasing time from string
peo[i].full_name.erase(peo[i].full_name.begin() + 20,
peo[i].full_name.end());
}
sort(peo, peo + time, comp);
cout << endl;
for (int i = 0; i < time; i++) {
cout << peo[i].full_name << " " << peo[i].min << " " << peo[i].sec << endl;
}
return 0;
}
/*
input for example:
6
Petras A. Petraitis 0 20 00
Jurgis Jurgutis 0 12 59
Romas Jonas 0 15 12
Zigmas Nosis 0 23 9
Rimas Senasis 0 15 12
output I get:
em3╣Mg n Ç 0 0 //random numbers I get
Jurgis Jurgutis 12 59
Rimas Senasis 15 12
Romas Jonas 15 12
Petras A. Petraitis 20 0
Zigmas Nosis 23 9 */
"Write a program which reads n numbers and which determines a number c, the smallest of the read numbers which contains the largest digit found in them."
Example: n=10 ; numbers: 23 12 64 12 72 345 67 23 71 634 ; c=67 (the largest digit found in these numbers is 7 and it can be found in 72, 67, 71. The smallest number is 67)
I don't know why, but my program doesn't work. Every time, it shows me that c=0.
#include <iostream>
using namespace std;
int main()
{
int n, prec, crt, digitMax, t, a, i, c;
digitMax=0;
t=0;
cout<<"Give the number of numbers: "; cin>>n;
cout<<"Give the first number: "; cin>>prec;
do{
a=prec%10;
prec=(prec-a)/10;
if(a>digitMax){
digitMax=a;
}
} while(prec!=0);
for(i=1; i<n; i++){
cout<<"Give the next number: "; cin>>crt;
do{
a=crt%10;
crt=(crt-a)/10;
if(a>t){
t=a;
}
} while(crt!=0);
if(digitMax>t){
c=prec;
} else if(digitMax<t) {
digitMax=t;
c=crt;
} else if(prec>=crt){
c=crt;
} else{
c=prec;
}
prec=crt;
}
cout<<"c is "<<c;
}
Problem is that you are doing everything inside a single function make it very complex. The proper way to tackle problem is first define a small functions whcih are doing smaller tasks.
One which calculates biggest digit in number:
int BigestDigitOf(int x)
{
x = std::abs(x);
int biggestDigit = 0;
while (x)
{
biggestDigit = std::max(biggestDigit, x%10);
x/=10;
}
return biggestDigit;
}
And one which compares two numbers according to c definition:
bool BiggerDigitOrSmallerValue(int a, int b)
{
auto bdA = BigestDigitOf(a);
auto bdB = BigestDigitOf(b);
if (bdA > bdB) {
return true;
}
return bdA == bdB && a < b;
}
After that solving your problem is very simple (one line if STL is used).