Trouble with sorting 2d double array alongside 1d string array. - c++

This is a working program that I've been trying to learn C++ from that inputs from a file strings and doubles and puts them into their perspective arrays. What I'm stuck on is while sorting the strings I want to sort the array of doubles in ascending order and keep the scores with the name they are associated with. I'm trying to learn it without using vectors before I go on to learn how to manipulate them, that's the reason for not using vectors before anyone asks.
Is it best to sort the columns of the 2d array and than sort them with the 1d array or just to have a statement that does it all? Also what would be the best sorting algorithm for this application?
My attempts have all failed so far so I turn to the community here for help. The logic is most likely some simple concept that I have yet to grasp. We all have to start somewhere. Thank you in advance for your help.
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void ArraySort (string x[], double y[][3], int length);
int main()
{
ifstream inFile;
inFile.open("bowlers2.txt");
const int SIZE = 10;
int i,j;
double scores[10][3];
string names[SIZE];
string mystring;
if (!inFile)
{
cout << "Can not open the input file"
<< " This program will end."<< "\n";
return 1;
}
for(i = 0; i < SIZE; i++)
{
getline(inFile, names[i]);
for(j = 0; j < 3; j++)
{
getline(inFile, mystring);
scores[i][j] = atoi(mystring.c_str());
}
}
for(int i=0;i<SIZE;i++)
{
cout << names[i] << "\n";
for(j = 0; j < 3; j++)
cout << scores[i][j] << "\n";
}
inFile.close();
ArraySort (names, scores, SIZE);
return 0;
}
void ArraySort (string x[], double y[][3], int LENGTH)
{
int i,j;
string sValue;
double dValue;
for(i = 1; i < LENGTH; i++)
{
sValue = x[i];
for(j = i - 1; j >= 0 && x[j] > sValue; j--)
{
x[j + 1] = x[j];
}
x[j + 1] = sValue;
}
cout << "\n";
for(int i=0;i<LENGTH;i++)
{
cout << x[i] << "\n";
for(j = 0; j < 3; j++)
cout << y[i][j] << "\n";
}
}
file read by program:
Linus too good
100
23
210
Charlie brown
1
2
12
Snoopy
300
300
100
Peperment Patty
223
300
221
Pig Pen
234
123
212
Red Headed Girl
123
222
111
Marcey
1
2
3
keith hallmark
222
300
180
anna hallmark
222
111
211
roxie hallmark
100
100
2

I think what you’re trying to do is this:
void ArraySort (string x[], double y[][3], int LENGTH)
{
int i,j,k;
string sValue;
double dValue;
double dArray[3];
for(i = 1; i < LENGTH; i++)
{
sValue = x[i];
for (k = 0; k < 3; k++)
{
dArray[k] = y[i][k];
}
for(j = i - 1; j >= 0 && x[j] > sValue; j--)
{
x[j + 1] = x[j];
for (k = 0; k < 3; k++)
{
y[j + 1][k] = y[j][k];
}
}
x[j + 1] = sValue;
for (k = 0; k < 3; k++)
{
y[j + 1][k] = dArray[k];
}
}
for(k = 0; k < LENGTH; k++)
for(i = 1; i < 3; i++)
{
dValue = y[k][i];
for(j = i - 1; j >= 0 && y[k][j] > dValue; j--)
{
y[k][j + 1] = y[k][j];
}
y[k][j + 1] = dValue;
}
}
cout << "\n";
for(int i=0;i<LENGTH;i++)
{
cout << x[i] << "\n";
for(j = 0; j < 3; j++)
cout << y[i][j] << "\n";
}
}

For learning purposes you can use bubble sort. Bubble sort is very easy, also very inefficient and slow. In a real application you would use std::sort
template <typename T>
void bubble_sort(T *num, int num_count)
{
for (int i = 0; i < (num_count - 1); i++)
for (int j = i + 1; j < num_count; j++)
if (num[i] > num[j])
std::swap(num[i], num[j]);
}
void ArraySort(string str[], double num[][3], int str_count)
{
int num_count = 3;
bubble_sort(str, str_count);
for (int i = 0; i < str_count; i++)
bubble_sort(num[i], num_count);
cout << "\n";
for (int i = 0; i < str_count; i++)
{
cout << str[i] << "\n";
for (int j = 0; j < num_count; j++)
cout << num[i][j] << "\n";
}
}

Related

How to display this pyramid of numbers?

I have this task:
A user inputs a number N and you have to output this pyramid:
0
101
21012
.......
N.21012.N
For N=5 it will be :
0
101
21012
3210123
432101234
54321012345
I managed to only get it working for N<10 with this code:
int n;
cin >> n;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n - i; j++)
cout << " ";
int dir = -1;
for (int k = i; k <= i; k += dir) {
cout << k;
if (k == 0)
dir = 1;
}
cout << endl;
}
For N=10 it will look like this :
0
101
21012
3210123
432101234
54321012345
6543210123456
765432101234567
87654321012345678
9876543210123456789
10987654321012345678910
After the answers I settled on this :
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int n, spaces;
string number;
cin >> n;
if (n < 10)
spaces = n;
else
{
spaces = 9;
int pwr = 0, k = n;
while (k > 9)
{
pwr++;
k /= 10;
}
for (int i = 1; i < pwr; i++)
{
spaces += pow(10, i) * 9 * (i + 1);
}
spaces += (n - pow(10, pwr) + 1) * (pwr + 1);
}
// cout << spaces << endl;
for (int i = 0; i < n + 1; i++)
{
for (int j = i; j > -1; j--)
number += to_string(j);
int len = number.length() - 1;
for (int j = 0; j < spaces - len; j++)
cout << " ";
for (int j = 1; j <= i; j++)
number += to_string(j);
cout << number << endl;
number.clear();
}
cout << endl;
return 0;
}
int padding(int n) {
constexpr auto singleDigitNumbersCount = 9;
constexpr auto doubleDigitNumbersCount = 90; // from 10 to 99
if (n < 10) return n;
if (n < 100) return 2*n - singleDigitNumbersCount;
return 3*n - doubleDigitNumbersCount - 2*singleDigitNumbersCount;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n + 1; i++) {
std::cout << std::string(padding(n) - padding(i), ' ');
for (int k = i; k >= 0; k--) {
cout << k;
}
for (int k = 1; k <= i; k++) {
cout << k;
}
cout << '\n';
}
return 0;
}
https://godbolt.org/z/EEaeWEvf4
I made this a bit ago Compiler Explorer
Not sure if that'd help 🤔
Here is the working code:
#include <string>
#include <iostream>
using namespace std;
#define MAX_SPACE 50
int main()
{
int n;
cin >> n;
string output = "";
for (int i = 0; i < n + 1; i++)
{
for (int k = i; k >= 0; k--) {
output += to_string(k);
}
for (int k = 1; k <= i; k++) {
output += to_string(k);
}
for (uint8_t i = 0, max = MAX_SPACE - output.length() / 2.00; i < max; i++) // Print max spaces minus the integer length divided by 2
{
cout << " ";
}
cout << output << endl; // Print number
output = "";
}
return 0;
}

How do this program but in reverse, pattern

so i want output like this
1
123
12345
123
1
i already make the program but it only output these, and im confused how to output the bottom triangle
1
123
12345
here's my program
#include <iostream>
using namespace std;
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
#Mojtaba's answer is a perffect extension to your approach.
However, I wanted to provide another method that is generally used in creating such strings that are formatted in a particular manner. It is common to create the entire pattern line by line and then print to the console all at once.
I have appropriately commented the code for your reference and it should be easy to understand:
#include <iostream>
#include <vector>
void pattern(int n) {
std::vector<std::string> lines; // store the first n lines to print later
int length = 2*n - 1; // length of each line
for(int i = 0; i < n; i++) {
std::string str = std::string(length, ' ');
for(int j = 1; j <= 2*i + 1; j++) {
str[n - i + j - 2] = j + '0';
// indexing can be figured by observing the pattern
}
lines.emplace_back(str);
}
for(int i = 0; i < n; i++) {
std::cout << lines[i] << std::endl;
}
for(int i = n-2; i >= 0; i--) {
std::cout << lines[i] << std::endl;
}
return;
}
int main() {
int n;
std::cin >> n;
pattern(n);
}
I added another for loop exactly like yours with different order from n-1. I modified your code to this:
int main() {
int n = 3 ;
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
for (i = n - 1; i >= 1; i--) {
for (j = n; j > i; j--) {
cout << " ";
}
for (k = 1; k <= (2 * i - 1); k++) {
cout << k;
}
cout <<endl;
}
return 0;
}
Now it returns:
1
123
12345
123
1

image made from dots and letters c++

very basic Q as I just started with coding but I stuck at some point and have 0 ideas what to do.
I need to write code to get diamond shape made from dots and X letters, size based on a value (n) provided by the user, (3 ≤ n ≤ 80).
for example:
As I mentioned - I have almost 0 experience so all I could get is is this shape for n=6
height is ok, same as widht but unfortunately, the amount of X's and placement isn't correct :/
my code:
int h;
cerr << "Provide size of diamond: ";
cin >> h;
for (int i = 1; i <= h; i++)
{
for (int k = 1 ; k <= h-i ; k++)
{
cout << ".";
}
for (int j = 1; j <= i ; j++)
{
cout << "X";
}
cout << endl;
Thank you all good people who will help mi with this one :)
I help to draw points. I hope you, looking on my change, are able to update your code further to achive the required picture.
for (int i = 1; i <= h; i++)
{
for (int k = 1 ; k <= (h-i) / 2 ; k++)
{
cout << ".";
}
for (int j = 1; j <= i ; j++)
{
cout << "X";
}
for (int k = 1 ; k <= (h-i) / 2 ; k++)
{
cout << ".";
}
cout << endl;
}
In this kind of problems you can divide the problems in different parts. Such as for n=6 the image can be divided in 4 mirror images:
..X
.XX
XXX
then,
X..
XX.
XXX
and upside down mirror of them.
You said that you can draw the first one. I think if you give some more time you will be able to print the full image too.
But, if you have problems, here's the code for that
for (int i = 1; i <= h; i++) {
if((h-i)%2) continue;
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
for (int j = 1; j <= i ; j++) {
cout << "X";
}
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
cout << endl;
}
for (int i = (h/2)*2; i > 0; i--) {
if((h-i)%2) continue;
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
for (int j = 1; j <= i ; j++) {
cout << "X";
}
for (int j = 1 ; j <= (h-i) / 2 ; j++) {
cout << ".";
}
cout << endl;
}
Since this is tagged as a c++ question let's use std::string and three loops.
#include <iostream>
#include <string>
void print_diamond(int n)
{
int np = n / 2, nm = (n - 1) / 2;
int npl = np, nml = nm;
std::string str(n, '.');
for (int i = 0; i < nm; i++)
{
str[npl++] = 'X'; str[nml--] = 'X';
std::cout << str << std::endl;
}
for (int i = nm; i <= np; i++)
{
str[npl] = 'X'; str[nml] = 'X';
std::cout << str << std::endl;
}
for (int i = np; i < n - 1; i++)
{
str[npl--] = '.'; str[nml++] = '.';
std::cout << str << std::endl;
}
std::cout << std::endl;
}
Print all diamond for a shinier world...
int main()
{
for (int n = 3; n < 81; n++)
{
print_diamond(n);
}
}

Program stops after cin - C++

I know this might be a duplicate to another question on this forum but I couldn't find the solution for my problem, even if I searched for like 1 hour.
The problem is that my program stops after the 4th "cin". I don't know why, I tried everything: "cin.ingore(); cin.clear();", "cin.get();".
Could someone help me please?
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
struct elev
{
char nume[20];
vector<int> note_info;
float medie;
};
int main()
{
int n, e = 0;
vector<elev> elevi;
cout << "n = "; cin >> n;
for (int i = 1; i <= n; i++)
{
int s = 0, nr;
elevi.push_back(elev());
cout << "Nume elev: "; cin >> elevi[i].nume;
cout << "Numar note informatica: "; cin >> nr;
for (int j = 0; j < nr; j++)
{
int temp;
cout << "Nota nr. " << j + 1 << ": "; cin >> temp;
elevi[i].note_info.push_back(temp);
s += temp;
}
elevi[i].medie = (float)(s / nr);
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
if (elevi[j].medie != elevi[j + 1].medie)
{
e += 1;
}
}
}
if (e)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (elevi[j].medie < elevi[j + 1].medie)
{
elev temp = elevi[j];
elevi[j] = elevi[j + 1];
elevi[j + 1] = temp;
}
}
}
}
else
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (elevi[j].nume > elevi[j + 1].nume)
{
elev temp = elevi[j];
elevi[j] = elevi[j + 1];
elevi[j + 1] = temp;
}
}
}
}
cout << "Rezultate:";
for (int i = 1; i <= n; i++)
{
cout << '\n' << elevi[i].nume << ' ' << setprecision(2) << fixed << elevi[i].medie;
}
return 0;
}
Replace this line:
for (int i = 1; i <= n; i++)
with
for (int i = 0; i < n; ++i)
The error stems from trying to access the vector elevi at a position it doesn't yet have. Because vectors start indexing at 0, the first access made to elevi should be at index 0.

How do i add all the values in my ascending array?

First i need to re-arrange all the values of my array into ascending order then add it afterwards. For example the user input 9 2 6, it will display in ascending order first ( 2 6 9 ) before it will add the sum 2 8 17.. The problem is my ascending order is not working, is there something wrong in my code?
#include <iostream>
#include<conio.h>
using namespace std;
int numberof_array, value[10], temp;
int i = 0, j;
void input()
{
cout << "Enter number of array:";
cin >> numberof_array;
for (i = 0; i < numberof_array; i++)
{
cout << "Enter value for array [" << i + 1 << "] - ";
cin >> value[i];
cout << endl;
}
}
void computation()
{
// this is where i'll put all the computation
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
for (i = 0; i <= numberof_array; i++)
{
for (j = 0; j <= numberof_array - i; j++)
{
if (value[j] > value[j + 1])
{
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
}
}
}
void display()
{
// display all the computation i've got
cout << "\nData after sorting: ";
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
getch();
}
int main()
{
input();
computation();
display();
}
void computation(){
for (int j = 0; j < numberof_array; j++) cout << value[j]<<"\t";
for (int i = 0; i <= numberof_array; i++) {
temp = value[i];
int temp_idx = i;
for (int j = i; j < numberof_array; j++) {
if (value[j] < temp) {
temp = value[j];
temp_idx = j;
}
}
int temp_swap = value[i];
value[i] = value[temp_idx];
value[temp_idx] = temp_swap;
}
}
How about changing your second function to something like above.
I have to agree with other commentators that your coding style is not preferred but there might be more to the story than meets the eye.