How do you reverse a vector? I've read a lot of online post but I can't find one using namespace std. I need to use reverse() and vect.reverse();
Here is my code:
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
cout << "Kaitlin Stevers" << endl;
cout << "Exercise 11 - Vectors" << endl;
cout << "November 12, 2016" <<endl;
cout << endl;
cout << endl;
int size;
cout << " How many numbers would you like the vector to hold? " << endl;
cin >> size;
vector<int> numbers;
int bnumbers;
for (int count = 0; count < size; count++)
{
cout << "Enter a number: " << endl;
cin >> bnumbers;
numbers.push_back(bnumbers); // Adds an element to numbers
}
//display the numbers stored in order
cout << "The numbers in order are: " << endl;
for(int bcount = 0; bcount < size; bcount++)
{
cout << numbers[bcount] << " ";
}
cout << endl;
//display the numbers stored reversed
reverse(numbers.begin(), numbers.end());
return 0;
}
You need to include <algorithm> to have access to std::reverse.
The last line in your code will work as is, if you include the header.
Related
#include <iostream>
#include <vector>
using namespace std;
int main() {
int val = 8;
vector<int>numVal(val);
unsigned int i;
cout << "Enter " << val << " integers: "<< endl;
for(i = 0; i < numVal.size(); i++)
{
cin >> val;
}
cout << "Congratulations! You entered 8 integers." << endl;
for(i = 0; i < numVal.size(); i++)
{
cout << numVal.at(i)<<" ";
}
return 0;
}
You are initializing the vector with 8 default-valued elements, and then you are ignoring the user's input completely. That is why you are printing zeros.
You need to store the user's input in the vector, either by:
using the vector's operator[] to access the pre-allocated elements:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numVal(8);
size_t i;
cout << "Enter " << numVal.size() << " integers: "<< endl;
for(i = 0; i < numVal.size(); i++)
{
cin >> numVal[i];
}
cout << "Congratulations! You entered " << numVal.size() << " integers." << endl;
for(i = 0; i < numVal.size(); i++)
{
cout << numVal[i] << " ";
}
return 0;
}
not pre-filling the vector's elements, but using its push_back() method instead:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int val;
vector<int> numVal;
size_t i;
numVal.reserve(8);
cout << "Enter " << numVal.capacity() << " integers: "<< endl;
for(i = 0; i < numVal.capacity(); i++)
{
cin >> val;
numVal.push_back(val);
}
cout << "Congratulations! You entered " << numVal.size() << " integers." << endl;
for(i = 0; i < numVal.size(); i++)
{
cout << numVal[i] << " ";
}
return 0;
}
I am trying to add up all the values that have been stored into array b and have it display under the "total column" and don't know how to only have the scores add together.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int array[5][4];
int sum = 0;
cout<<"Enter grades for 4 exams for the 5 students \n";
for(int i=0;i<5;i++)
{
for(int b=1;b<=4;b++)
{
cout <<setw(8)<< "enter student "<< i << "'s grade for exam " << b << '\n';
cin >> array[i][b];
}
}
cout <<"ID"<<setw(11)<<"score 1"<<setw(11)<<"score 2"<<setw(11)<<"score 3"<<setw(11)<<"score 4"<<setw(11)<<"total"<<setw(11)<<"letter"<<endl;
cout <<"--------------------------------------------------------------------------------------------------------------\n";
for(int i=0;i<5;i++)
{
cout << i<< " ";
for(int b=1;b<=4;b++)
{
sum = sum + array[b];
cout <<setw(10)<<array[i][b]<<sum;
}
cout <<'\n';
}
cout <<"--------------------------------------------------------------------------------------------------------------\n";
return 0;
}
To be more specific around line 28
for(int i=0;i<5;i++)
{
cout << i<< " ";
for(int b=1;b<=4;b++)
{
sum = sum + array[b];
cout <<setw(10)<<array[i][b]<<sum;
}
cout <<'\n';
Arrays indexes start at 0, not 1. You are correctly looping through your array's 1st dimension, but not its 2nd dimension. You need to change the inner for loops from for(int b=1;b<=4;b++) to for(int b=0;b<4;b++)
Also, to handle the total column, you simply need to reset sum to 0 on each iteration of the 1st dimension, and then print the result after the iteration of the 2nd dimension.
Try this:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int array[5][4];
cout << "Enter grades for 4 exams for the 5 students \n";
for(int i = 0; i < 5; i++)
{
for(int b = 0; b < 4; b++)
{
cout << "Enter student " << i+1 << "'s grade for exam " << b+1 << '\n';
cin >> array[i][b];
}
}
cout << "ID" << setw(11) << "score 1" << setw(11) << "score 2" << setw(11) << "score 3" << setw(11) << "score 4" << setw(11) << "total" << setw(11) << "letter" << endl;
cout << "--------------------------------------------------------------------------------------------------------------\n";
for(int i = 0; i < 5; i++)
{
cout << setw(2) << left << i+1 << right;
int sum = 0;
for(int b = 0; b < 4; b++)
{
sum = sum + array[i][b];
cout << setw(11) << array[i][b];
}
cout << setw(11) << sum << '\n';
}
cout << "--------------------------------------------------------------------------------------------------------------\n";
return 0;
}
Online Demo
This is my header file
#ifndef KINGDOM_H_
#define KINGDOM_H_
// TODO: sict namespace
using namespace std;
namespace sict {
// TODO: define the structure Kingdom in the sict namespace
class Kingdom {
public:
char m_name[32];
int m_population;
};
// TODO: declare the function display(...),
void display(const Kingdom&);
void display(Kingdom pKingdom[], int kingdomNum);
// also in the sict namespace
}
#endif
and these two are my cpp files
#include <iostream>
#include <string>
#include "Kingdom.h"
using namespace std;
// TODO: the sict namespace
namespace sict {
// TODO:definition for display(...)
void display(const Kingdom& pKingdom) {
cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;
}
void display(Kingdom pKingdom[], int kingdomNum) {
int total = 0;
cout << "------------------------------" << endl;
cout << "Kingdoms are" << endl;
for (int i = 0; i < kingdomNum; i++) {
cout << i + 1 << ". " << pKingdom[i].m_name << ", population" << pKingdom[i].m_population << endl;
total += pKingdom[i].m_population;
}
cout << "------------------------------" << endl;
cout << "Total population of SICT: " << total << endl;
cout << "------------------------------" << endl;
}
}
#include <iostream>
#include "Kingdom.h"
using namespace std;
using namespace sict;
void read(Kingdom&);
int main() {
int count = 0; // the number of kingdoms in the array
// TODO: declare the pKingdom pointer here (don't forget to initialize it)
Kingdom* pKingdom = nullptr;
cout << "==========\n"
<< "Input data\n"
<< "==========\n"
<< "Enter the number of Kingdoms: ";
cin >> count;
cin.ignore();
if (count < 1) return 1;
// TODO: allocate dynamic memory here for the pKingdom pointer
pKingdom = new Kingdom[count];
for (int i = 0; i < count; ++i) {
cout << "Kingdom #" << i + 1 << ": " << endl;
// TODO: add code to accept user input for Kingdom i
cout << "Enter the name of the Kingdom: ";
cin >> pKingdom[i].m_name;
cout << "Enter the number of people living in " << pKingdom[i].m_name << ": ";
cin >> pKingdom[i].m_population;
}
cout << "==========" << endl << endl;
// testing that "display(...)" works
cout << "------------------------------" << endl
<< "The 1st kingdom entered is" << endl
<< "------------------------------" << endl;
display(pKingdom[0]);
cout << "------------------------------" << endl << endl;
// add the new Kingdom
cout << "==========\n"
<< "Input data\n"
<< "==========\n"
<< "Kingdom #" << count + 1 << ": " << endl;
// TODO: accept input for the new element in the array
for (int i = 0; i < count; i++) {
count = 1;
cout << "Enter the name of the Kingdom: ";
cin >> pKingdom[i].m_name;
cout << "Enter the number of people living in " << pKingdom[i].m_name << ": ";
cin >> pKingdom[i].m_population;
}
count++;
cout << "==========\n" << endl;
// testing that the overload of "display(...)" works
display(pKingdom, count);
cout << endl;
// TODO: deallocate the dynamic memory here
delete[] pKingdom;
system("pause");
return 0;
}
// read accepts data for a Kingdom from standard input
//
void read(Kingdom& kingdom) {
cout << "Enter the name of the Kingdom: ";
cin.get(kingdom.m_name, 32, '\n');
cin.ignore(2000, '\n');
cout << "Enter the number of people living in " << kingdom.m_name << ": ";
cin >> kingdom.m_population;
cin.ignore(2000, '\n');
}
and the output should ask the user for number of kingdoms and depending on the number of kingdoms,
it will ask the number of same two questions which are asking for the name of kingdom and number of people living in the kingdom repeatedly until the loop is finished.
Now once the console proceeds to Kingdom#3 and the user types everything, at the last procedure where it would state all the kingdoms's names and population that belong in SICT namespace,
but the code won't put the Kingdom #3's information and the total population would be calculated without Kingdom #3's information.
So, it should look like this :
Kingdoms of SICT
The_Vale, population 234567
The_Reach, population 567890
3. The_Riverlands, population 123456
Total population of SICT: 925913
but it only prints
Kingdoms of SICT
The_Vale, population 234567
2. The_Reach, population 567890
Total population of SICT: 603,428
Can anyone tell me why and how to fix the problem?
Your display function is fine, but your input is a mess...
You should be using a std::vector or list as other comments mentioned, but if you have to use a raw array, you need to remember where in the array you're changing data, and to allocate a new array and copy the data when you need it to be bigger.
If your second round of input is only supposed to read one element, it shouldn't be a loop.
You should also be using your read function instead of duplicating your input code, and likewise, you should be using your "Display(...)" function in your "display all" function instead of duplicating code.
I get an error saying: use of undeclared identifier 'again'.
I am trying to go from int main to void again and back after getting an answer.
Please explain to me why it won't work also. Thanks.
Here is my full code:
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <string>
using namespace std;
{
string answer;
cout << "Would you like to enter another set of data? Y or N?" << endl;
cin << answer;
string yes = "Yes";
string no = "No";
if(a == yes)
{
main();
}
}
int main()
{
cout << "Kaitlin Stevers" << endl;
cout << "Exercise 11 - Vectors" << endl;
cout << "November 12, 2016" <<endl;
cout << endl;
cout << endl;
int size;
cout << " How many numbers would you like the vector to hold? " << endl;
cin >> size;
vector<int> numbers;
int bnumbers;
for (int count = 0; count < size; count++)
{
cout << "Enter a number: " << endl;
cin >> bnumbers;
numbers.push_back(bnumbers); // Adds an element to numbers
}
//display the numbers stored in order
cout << "The numbers in order are: " << endl;
for(int bcount = 0; bcount < size; bcount++)
{
cout << numbers[bcount] << " ";
}
cout << endl;
//display the numbers stored reversed
cout << "Here are the numbers in reverse order: " << endl;
reverse(numbers.begin(), numbers.end());
for(int rcount = 0; rcount < size; rcount++)
{
cout << numbers[rcount] << " ";
}
cout << endl;
again();
return 0;
}
void again()
}
You need to declare your fonction "again" before calling it :
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <string>
using namespace std;
void again();
int main()
{
cout << "Kaitlin Stevers" << endl;
cout << "Exercise 11 - Vectors" << endl;
cout << "November 12, 2016" <<endl;
cout << endl;
cout << endl;
int size;
cout << " How many numbers would you like the vector to hold? " << endl;
cin >> size;
vector<int> numbers;
int bnumbers;
for (int count = 0; count < size; count++)
{
cout << "Enter a number: " << endl;
cin >> bnumbers;
numbers.push_back(bnumbers); // Adds an element to numbers
}
//display the numbers stored in order
cout << "The numbers in order are: " << endl;
for(int bcount = 0; bcount < size; bcount++)
{
cout << numbers[bcount] << " ";
}
cout << endl;
//display the numbers stored reversed
cout << "Here are the numbers in reverse order: " << endl;
reverse(numbers.begin(), numbers.end());
for(int rcount = 0; rcount < size; rcount++)
{
cout << numbers[rcount] << " ";
}
cout << endl;
again();
return 0;
}
void again()
{
string answer;
cout << "Would you like to enter another set of data? Y or N?" << endl;
cin >> answer;
string yes = "Yes";
string no = "No";
if(answer == yes)
{
main();
}
}
However, it's a strange things to use recursivity for what you want to do and to call main func multiple times (which is by definition, main entry of a program). In my opinion, you should call in your main function another function named like askInformation which uses a loop with a test case to know if user wants to add informations or not.
Hi I'm working on a project that lets the user input numbers and the code organizes them in order of least to greatest and tells how many of each number was input. I'm having problems with the organizing of least to greatest and counting how many of each number are inputed
using namespace std;
#include <iostream>
void main(){
double a[30];
double e[30];
double b;
int c[30];
int d;
double f=0;
cout << "how many input values [max 30]:";
cin >> d;
cout << "enter " << d << " numbers:"<<endl;
for(int x=0; x<d;x++){
cin >> a[x];
c[x]=0;
}
cout << endl;
for(int y=0; y<d;y++){
if(a[y]>=f){
f=a[y];
}
}
for(int z=0;z<d;z++){
if(a[z]){
c[z]++;
}
if(a[z]>=a[z+1]){
e[z]=a[z];
}
}
cout << "numbers count"<< endl;
for(int printloop=0;printloop<d;printloop++){
if(a[printloop]>0){
cout << e[printloop]<< " " << c[printloop] << endl;
}
}
cout << "max value:" << b << endl;
}
Solution base on std::map http://en.cppreference.com/w/cpp/container/map, note the Compare template argument and C++11 (http://en.cppreference.com/w/cpp/language/range-for)
#include <iostream>
#include <map>
int main(int argc, char** argv)
{
std::map<int, int> count;
int n;
std::cout << "How many numbers ? ";
std::cin >> n;
std::cout << "Now enter " << n << " numbers : ";
for(auto i=0; i<n; i++)
{
int tmp;
std::cin >> tmp;
count[tmp]++;
}
for(auto const& elem : count )
std::cout << "Element " << elem.first << " count : " << elem.second << "\n";
return 0;
}