Getting all pairs of numbers in a 2D array c++ - c++

I am needing to print out all pairs of numbers from each line read in from a text document. A sample text document would be:
6 8
1 3 5
2 3 4
3 6 5
7 6 8
4 6
7 5
Where the first line is the number of nets (6) and the number of cells (8) for a hypergraph. The rest of the lines are the cells that are in the net. So net 1 consists of cells 1, 3, and 5, net 2 consists of cells 2, 3, and 4 and so on. In order to turn this netlist into an actual graph I need to go through each line and basically take all combinations of the numbers on each line. So after reading in the first net I would like to be able to make a graph with (1,3), (1,5), and (3,5) and then go down the netlist and add to the graph. So far I am able to read everything in from the text file and print out the individual cells that I put into a 2D array. Here is my code for that:
int main() {
ifstream infileHGR; // set stream for hypergraph text file
string inputFileName = "structP.hgr"; // input hypergraph filename here
infileHGR.open(inputFileName, ios::in);
clock_t start = clock(); // start clock
string line;
string data[2]; // initialize data array to take in # of nets and # of cells
int nets = 0;
int cells = 0;
// Reads in the first line of the text file to get # for nets and cells
getline(infileHGR, line);
stringstream ssin(line);
int i = 0;
while (ssin.good() && i < 2) { // error checking to make sure first line is correct format
ssin >> data[i];
i++;
}
nets = atoi(data[0].c_str()); // set first number to number of nets
cells = atoi(data[1].c_str()); // set second number to number of cells
freopen("output.txt", "w", stdout); // writes outptut to text file
// TESTING PURPOSES
cout << "Number of nets = " << nets << endl;
cout << "Number of cells = " << cells << endl;
// while loop to go through rest of the hgr file to make hypergraph (starts at line 2)
string str;
int count = 1; // counter for nets
while (infileHGR.good()) {
getline(infileHGR, str);
stringstream in(str);
int i = 0;
// have the line in str
int n = 1; // start at 1, spaces + 1 = number of nodes per net
for (int i = 0; i < str.length(); ++i) {
if (str.at(i) == ' ') {
n++; // n is number of cells in the net
}
}
// testing
//cout << "str = " << str << endl;
//cout << "n = " << n << endl;
int number;
vector<vector<int> > netList;
vector<int> temp;
while (in >> number){
temp.push_back(number);
}
netList.push_back(temp);
//printNetList(temp); // test to see if info is being put into the vectors
// loop through the 2d vector
for (const auto& inner : netList) {
cout << "net " << count << " = "; //TESTING PURPOSES
for (const auto& item : inner) {
cout << item << " ";
}
count = count + 1;
}
cout << endl;
}
clock_t stop = clock(); // end clock
infileHGR.close();
double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
printf("Time elapsed in ms: %f", elapsed);
system("pause"); //for original testing
return 0;
}
I used vectors because every input file will be of different size, and some contain a lot of nets, and some nets have up to 20 cells in them. I need help with getting all pairs (coordinates) from the netlist and printing them out to show all of them. I have messed around with the for loops a lot but can't seem to get something that works. Any help would be greatly appreciated, and just ask if I need to include anything else. Thank you!

If you're looking to print out all of the possible indexes from an array. You are looking in the right direction. You can do it with for loops, and I actually had this issue a bit back for an assignment. Take a look at this, it should return all possible indexes:
int b, c = 0;
int userIndex_A;
int userIndex_B;
cin >> userIndex_A;
cin >> userIndex_B;
//This is so you can have variable array lengths
int Arr[userIndex_A][userIndex_B];
for(int a = 0; c < 10; a++){
//The reason I put c in the for loop, to tell it when to stop, is because when c is equal to 10, that's the last index being paired
cout << "Array Index: "<< b << ", "<< c << endl;
if(b == (userIndex_A - 1)){
//userIndex-A is the array length, but 10 doesn't exist, so subtract 1
b = 0;
c++;
//if b is equal to max index value entered, set it to zero and add one to c. You only add one to c when you want to start with the next set of index.
}
b++;
//after each loop, increment the variables
}
This also works for 3D and 4D arrays too, just add more variables to increment each loop and set up for loops to reset the variable once it reachs the respective max array index length.

Looking at your example,
for each line
for i = 1 to N-1th element
for j = i+1 to Nth element
print (i,j)

I'll post my answer here because I was able to figure it out thanks to some suggestions. Also thank you for all the feedback on the rest of the code, I have definitely made it better since the original post. My for loop though for looping through the 2D vector and printing out all of the pairs is this (you can just ignore the weight variable being output):
for (vector<vector<int>> ::iterator i = netList.begin(); i != netList.end(); ++i) {
for (vector<int> ::iterator j = temp.begin(); j != temp.end() - 1; ++j) {
for (auto k = temp.begin() + 1; k != temp.end(); ++k) {
if (*j != *k) {
cout << *j << " " << *k << " " << weight << endl;
}
}
}
}

Related

How do i make my array compare and then display certain outputs based on users guess

Newer to coding and im stuck, Need to make a array thats stores 5 numbers (1-9), then i need to check that array for duplicates if there is duplicates i need to replace that number either with a whole new random line no duplicates (seems like the easier option) or just replace that one number,
after that wants me to get users 5 numbers guess store that in a array, display that array at the bottom along with these under each of the numbers
// The * = means the number is in the exact location
// The - = means the array does not contain that number
// The + = means the array contains the number but its not in the right location
All the arrays want the digits to be entered one at a time
repeat steps till user gets all the numbers to * then end game with completion msg.
My true problem lies with step 4 and step 7;
Below is the code ive been working on today but any help would be truly appreciated
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void game_instructions(int n);
bool search_array(int a[], int n, int t);
int main()
{
//Step 1: Declare your variables and constants
const int SIZE = 5; //this constant stores the length/size of the code
int randcode[SIZE]; //this array stores the code generated randomly by the computer
int guess[SIZE]; //this array stores the code inputted by the player/user
//you may add more variables as needed
//Step 2: Output game instructions by calling function game_instructions
game_instructions(SIZE);
//Step 3: Generate a random code and store it in the array randcode one digit at a time.
//Each digit should be between 0 and 9 and should be stored as one array element
//Recall that rand() % 10 can be used to generate a number between 0 and 9
srand(time(0));
for(int i=0;i<SIZE;i++)
randcode[i]= (rand() % 10); //Computers random 5 numbers generated
cout<<"\nRandom C-numbers::"<<endl;
for(int i=0;i<SIZE;i++)
cout<<randcode[i] << " ";
//Step 4: Repeat step 3 if the array randcode contains duplicates
//You must use function contains_duplicates to implement this step
//Step 5: Ask the user for his guess and store it in the array guess.
//Read one digit at a time, validate it to make sure it is between 0 and 9, and store it as one array element
for (int i=0; i<SIZE; i++) {
cout<<"\nEnter Digit "<< i+1 << ": ";
cin >> guess[i];}
cout << endl;
//Step 6: Output the array guess on a single line with a space after each element (see the sample output provided)
for (int n=0; n < SIZE; ++n) {
cout << guess[n] << " ";
}
//Step 7: Compare the randomly generated code (randcode) with the user's guess (guess)
//and display feedback for each digit as: *, + or –, as explained below:
//For each digit in the user's guess do the following:
// If it matches the digit from the random code (both value and position), output *
// Otherwise, if it appears anywhere in the random code, output + (use function search_array here)
// Otherwise, output -
//Step 8: Repeat steps 5,6,7 until all digits have been guessed correctly
//Step 9: Output congratulations message
cout << endl << endl;
cout << "Good job! You guessed the code!";
return 0;
}
void game_instructions(int n)
//This function outputs the game instructions.
//Its parameter n represents the length of the code.
{
cout << "A random " << n << " digit code has been generated. You have to guess it. \n";
cout << "For every digit you will receive feedback in the form of *, + or - \n";
cout << " * means the digit is in the code and it is in the correct position.\n";
cout << " + means the digit is in the code but it is not in the correct position.\n";
cout << " - means the digit is not in the code.\n";
}
bool search_array(int a[], int n, int t)
//This function searches the array a (of size n) for a target value t.
//If t is found in the array the function returns true; otherwise it returns false.
{
for (int i = 0; i < n; i++)
{
if (a[i] == t) return true;
}
return false;
}
bool contains_duplicates(int a[], int n)
//This function searches the array a (of size n) and returns true if the array contains duplicates; otherwise, it returns false.
{
for (int i = 0; i < n; i++)
{
//compare element a[i] with all the remaining elements from index i+1 to n
for (int j = i+1; j < n; j++)
{
if (a[i] == a[j]) return true;
}
}
return false;
}
I got most the side code done but im just stumped on how to get this array to match any help would be nice
Here is how I would implement the program to generate the number without duplicate:
Step3 would look like this:
int tempNumber;
int i = 0;
while (i < SIZE) {
tempNumber = (rand() % 10);
if (contains_duplicates(randcode, tempNumber, i) == false) {
// every time there is no duplicate we push and add i by 1;
randcode[i] = tempNumber; // Computers random 5 numbers generated
i++;
}
}
contains_duplicates function would look like this:
bool contains_duplicates(int arr[], int tempNum, int currentArrSize)
// This function searches the array a (of size n) and returns true if the
//array
// contains duplicates; otherwise, it returns false.
{
for (int j = 0; j <= currentArrSize; j++) {
// if the array[index] not equal generated number the loop will
//iterate again without going to the line below
if (arr[j] != tempNum) {
continue;
}
// if the array[index] equal the function will return true and end
//the function
// uncomment this to check how many time it duplicate (not
//necessary tho)
//cout << "if return true, this will appear"<< endl;
return true;
}
// if the loop is done and no duplicate, function will return false
return false;
}
With this method it is not necessary to do step 4 because we already prevent duplicate array!
Here is the last problem from your question which is step 7:
cout << endl;
for (int i = 0; i < SIZE; i++) {
if (guess[i] == randcode[i]) {
cout << "* ";
} else if (search_array(randcode, SIZE, guess[i])) {
cout << "+ ";
} else {
cout << "- ";
}
}
Seems like the problem has been solved, now try to implement step 8 by yourself, good luck :)

Garbage values are being output in 2D Arrays [duplicate]

I got this output
Learning how to use arrays and I got an exercise to check if the array is in an ascending order, after the first run, this is what I got and I cant find the problem.
when I'm printing the array it shows in the last array some garbage.
#include <iostream>
using namespace std;
const int CAPACITY1 = 5;
int main()
{
int arr1[CAPACITY1];
bool a = false;
//init the first input
cout << "Enter 6 numbers: " << endl;
cin >> arr1[0];
int max = arr1[0];
int NoE = 1;
//init the loop to insert numbers
for (int i = 1; i <= CAPACITY1; i++) {
cout << "Enter 6 numbers: " << endl;
cin >> arr1[i];
NoE++;
//check if input is bigger than max
if (arr1[i] > max)
max = arr1[i];
else
a = true;
}
if (a == true)
cout << "The array is not in an ascending order" << endl;
else
cout << "The array is in an ascending order" << endl;
cout << NoE;
//end
cout << endl;
return 0;
}
Indexes in arrays in C++ are zero based. That means, that array with capacity of N, declared as T arr[N], have indexes starting at 0 and ending with N - 1.
For accessing such array using for loop, use this
for (int index = 0; index < N; ++index)
Your loop here operates from 1 to 5:
for (int i = 1; i <= CAPACITY1; i++) {
cout << "Enter 6 numbers: " << endl;
cin >> arr1[i];
So you only enter five numbers and you never put anything in arr1[0] and you do put something in arr[5] which is invalid.
Aside from that, I suspect you may have missed the point of the exercise. If this is a homework assignment then I imagine they want you to iterate over an existing array and report if it is in ascending order.
Your for loop is wrong, and is going out of bounds.Note that arrays in C++ start at 0, not 1. The correct for loop is as follows:
for (int i = 0; i < CAPACITY1; i++) {
cout << "Enter 6 numbers: " << endl;
cin >> arr1[i];
NoE++;
//check if input is bigger than max
if (arr1[i] > max)
max = arr1[i];
else
a = true;
}
These 2 lines:
int CAPACITY1 = 5;
int arr1[CAPACITY1];
mean that the size of your array arr1 is 5, that is, it has 5 elements, from 0 to 4. Trying to access the element at position 5 is wrong.
That said, in your cout messages you talk about 6 numbers. If you want to store 6 elements, change the value of CAPACITY1 to 6, like:
const int CAPACITY1 = 6;
By your comments I think you are getting confused with the size of the array, possibly because you have heard something like "The index of the last element is always off by 1". Yes it is, but it doesn't mean that to store 6 elements you should use the number 5 when you define it! If you want 6 elements, the size must be 6, there is no trap here. The tricky part is that the index will start from 0, and therefore the last element is at position [size - 1], not at [size]. To clarify:
If you declare int arr1[5]; you will have 5 elements, with the index ranging from 0 to 4.
If you declare int arr1[6]; you will have 6 elements, with the index ranging from 0 to 5.
By the way, this shows why it would be a good idea to avoid hard-coded numbers (6, in this case) and always use variables. Try with
cout << "Enter " << CAPACITY1 << " numbers: " << endl;
This way, the size of the array and the messages will always match.
To summarize, if you want to work with 6 numbers, you need to use
const int CAPACITY1 = 6;
and, since you deal with the first element (at index 0) before the loop, the loop must start at 1 and run until the last element, which has index 5, so:
for (int i = 1; i < CAPACITY1; i++) {
On a side note, your logic to check whether the array is in ascending order is confusing. For you, a == true means it is not ascending. It works, but it is counter-intuitive. I would suggest to change it.

How to store user inputs in an array and check if the two array has the same element

I'm trying to make a quiz checker where the user will input their answer and it will automatically check in the array. Any tips?
Here is my code:
int main()
{
string aswer[] = { "D", "C", "D", "D", "A"); //
char input[5];
int counter ;
int points = 0;
cout << "Welcome and Good luck!\n";
for (counter = 0; counter < 5; counter++) {
cout << counter << " Choose from letters A-D: \n";
cin >> input;
}
for (int i = 0; i < 5; ++i) {
cout << "\n you enter " << input[i] << endl;
}
foreach (char item in answer){
foreach (char item1 in inputs){
If (item == item1){
points = points +1
}
}
}
return 0;
}
Tips -- be consistent choosing storage. If you are using a std::string for the answer (not the erroneous array of std::string shown), then use a std::string for input as well. But if you must use a POA (plain old array), that's fine for educational purposes -- you just lack all auto-memory management and bounds protections.
You initialize a std::basic_string as:
std::string answer { "DCDDA" }; /* initialize string (not array of strings) */
(note: the use of std:: namespace identifier. See Why is “using namespace std;” considered bad practice?)
Don't use Magic Numbers in your code. The 5 you stick in your array declaration and in your loop limit is a Magic Number. Instead,
#define NCHAR 5 /* if you need a constant, #define one (or more) */
...
char input[NCHAR]; /* array of 5 char (why not string?) */
...
for (int i = 0; i < NCHAR; i++) { /* loop taking input */
When you loop taking input, validate every input. The user may well generate a manual EOF to cancel input with Ctrl+d (or Ctrl+z on windows). Only increment the count of characters stored after you have validated the input, e.g.
int counter = 0, points = 0; /* counter and points, initialized zero */
std::cout << "Welcome and Good luck!\n\n";
for (int i = 0; i < NCHAR; i++) { /* loop taking input */
std::cout << " " << i << ". Choose from letters A-D: ";
if (std::cin >> input[i]) /* validate input */
counter++; /* only increment on successful input */
}
For your output, understand the difference between using '\n' and std::endl, C++: “std::endl” vs “\n”.
When you have items stored in a container (such as std::string), you can use a Range-based for loop (since C++11) to loop over each item in the container. To loop over your stored input, you must limit the loop to the number of elements successfully read as input. Use the same counter as your comparison limit, e.g.
for (auto& item : answer) /* range based for-loop over chars in string */
for (int i = 0; i < counter; i++) /* loop over counter chars in array */
if (item == input[i]) /* if the item matches the input char */
points += 1; /* add point */
Putting it altogether, you would have:
#include <iostream>
#include <string>
#define NCHAR 5 /* if you need a constant, #define one (or more) */
int main()
{
std::string answer { "DCDDA" }; /* initialize string (not array of strings) */
char input[NCHAR]; /* array of 5 char (why not string?) */
int counter = 0, points = 0; /* counter and points, initialized zero */
std::cout << "Welcome and Good luck!\n\n";
for (int i = 0; i < NCHAR; i++) { /* loop taking input */
std::cout << " " << i << ". Choose from letters A-D: ";
if (std::cin >> input[i]) /* validate input */
counter++; /* only increment on successful input */
}
std::cout << "\nYou entered:\n"; /* output stored values */
for (int i = 0; i < counter; i++) /* looping only 0 to counter */
std::cout << " input[" << i << "] : " << input[i] << '\n';
for (auto& item : answer) /* range based for-loop over chars in string */
for (int i = 0; i < counter; i++) /* loop over counter chars in array */
if (item == input[i]) /* if the item matches the input char */
points += 1; /* add point */
std::cout << "\nTotal points: " << points << '\n'; /* output total points */
}
Example Use/Output
$ ./bin/points
Welcome and Good luck!
0. Choose from letters A-D: A
1. Choose from letters A-D: B
2. Choose from letters A-D: C
3. Choose from letters A-D: D
4. Choose from letters A-D: E
You entered:
input[0] : A
input[1] : B
input[2] : C
input[3] : D
input[4] : E
Total points: 5
Checking maximum points if all characters match:
$ ./bin/points
Welcome and Good luck!
0. Choose from letters A-D: D
1. Choose from letters A-D: C
2. Choose from letters A-D: D
3. Choose from letters A-D: D
4. Choose from letters A-D: A
You entered:
input[0] : D
input[1] : C
input[2] : D
input[3] : D
input[4] : A
Total points: 11
Look things over and let me know if you have questions.

Why does the following code crash when I input a 12 digit number? [duplicate]

This question already has answers here:
cin >> fails with bigger numbers but works with smaller ones?
(4 answers)
Closed 5 years ago.
I have been following a course about algorithms on Coursera and I tried to put what I learned into code. This is supposed to be a "divide & conquer" algorithm and I hope that part is alright. I have a problem I encountered just messing around with it: everything works fine until I input a 12 digit number into the program. When I do that, it just ends the cin and outputs all the previous numbers sorted (blank space if no numbers are before). If you could, please tell me what's wrong if you spot the mistake. This is my code:
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
// setup global variable for the number of inversions needed
int inversions = 0;
// function to merge 2 sublists into 1 sorted list
vector<int> Merge_and_Count(vector<int>& split_lo, vector<int>& split_hi) {
// setup output variable -> merged, sorted list of the 2 input sublists
vector<int> out;
int l = 0;
int m = 0;
// loop through all the elements of the 2 sublists
for (size_t k = 0; k < split_lo.size() + split_hi.size(); k++) {
// check if we reached the end of the first sublist
if (l < split_lo.size()) {
// check if we reached the end of the second sublist
if (m < split_hi.size()) {
// check which element is smaller and sort accordingly
if (split_lo[l] < split_hi[m]) {
out.push_back(split_lo[l]);
l++;
}
else if (split_hi[m] < split_lo[l]) {
out.push_back(split_hi[m]);
m++;
inversions++;
}
}
else {
out.push_back(split_lo[l]);
l++;
inversions++;
}
}
else {
out.push_back(split_hi[m]);
m++;
}
}
return out;
}
// function that loops itself to split input into halves until it reaches the base case (1 element array)
vector<int> MergeSort_and_CountInversions(vector<int>& V) {
// if we reached the base case, terminate the loop and feed the output to the previous loop to be processed
if (V.size() == 1) return V;
// if we didn't reach the base case
else {
// continue halving the sublists
size_t const half_size = V.size() / 2;
vector<int> split_lo(V.begin(), V.begin() + half_size);
vector<int> split_hi(V.begin() + half_size, V.end());
// feed them back into the loop
return Merge_and_Count(MergeSort_and_CountInversions(split_lo), MergeSort_and_CountInversions(split_hi));
}
}
// main function of the app, runs everything
int main()
{
// setup main variables
int input;
vector<int> V;
// get input
cout << "Enter your numbers to be sorted (enter Y when you wish to proceed to the sorting)." << endl;
cout << "Note: do NOT use duplicates (for example, do not input 1 and 1 again)!" << endl;
while (cin >> input)
V.push_back(input);
cout << "\nThe numbers you chose were: " << endl;
for (size_t i = 0; i < V.size(); i++)
cout << V[i] << " ";
// get sorted output
vector<int> sorted = MergeSort_and_CountInversions(V);
cout << "\n\nHere are your numbers sorted: " << endl;
for (size_t j = 0; j < sorted.size(); j++)
cout << sorted[j] << " ";
// show number of inversions that were needed
cout << "\n\nThe number of inversions needed were: " << inversions << endl;
return 0;
}
12 decimal digits is too long to fit into a 32-bit number, which is how int is usually represented. Reading that number using >> therefore fails and cin >> input converts to a false value, which terminates the loop.
See operator >> documentation for details of handling failure modes.
You can get the maximum number of base-10 digits that can be represented by the type using the std::numeric_limits::digits10 constant:
std::cout << std::numeric_limits<int>::digits10 << '\n';
Chances are the maximum number of significant digits for type int is 9, and you try to supply 12 via standard input. The program doesn't crash, the condition of (cin >> input) simply evaluates to false.
12 digits is too much for 32-bit integer, try to use as unsigned long long int, check these limits:
http://www.cplusplus.com/reference/climits/

Array last element automatically gets garbage

I got this output
Learning how to use arrays and I got an exercise to check if the array is in an ascending order, after the first run, this is what I got and I cant find the problem.
when I'm printing the array it shows in the last array some garbage.
#include <iostream>
using namespace std;
const int CAPACITY1 = 5;
int main()
{
int arr1[CAPACITY1];
bool a = false;
//init the first input
cout << "Enter 6 numbers: " << endl;
cin >> arr1[0];
int max = arr1[0];
int NoE = 1;
//init the loop to insert numbers
for (int i = 1; i <= CAPACITY1; i++) {
cout << "Enter 6 numbers: " << endl;
cin >> arr1[i];
NoE++;
//check if input is bigger than max
if (arr1[i] > max)
max = arr1[i];
else
a = true;
}
if (a == true)
cout << "The array is not in an ascending order" << endl;
else
cout << "The array is in an ascending order" << endl;
cout << NoE;
//end
cout << endl;
return 0;
}
Indexes in arrays in C++ are zero based. That means, that array with capacity of N, declared as T arr[N], have indexes starting at 0 and ending with N - 1.
For accessing such array using for loop, use this
for (int index = 0; index < N; ++index)
Your loop here operates from 1 to 5:
for (int i = 1; i <= CAPACITY1; i++) {
cout << "Enter 6 numbers: " << endl;
cin >> arr1[i];
So you only enter five numbers and you never put anything in arr1[0] and you do put something in arr[5] which is invalid.
Aside from that, I suspect you may have missed the point of the exercise. If this is a homework assignment then I imagine they want you to iterate over an existing array and report if it is in ascending order.
Your for loop is wrong, and is going out of bounds.Note that arrays in C++ start at 0, not 1. The correct for loop is as follows:
for (int i = 0; i < CAPACITY1; i++) {
cout << "Enter 6 numbers: " << endl;
cin >> arr1[i];
NoE++;
//check if input is bigger than max
if (arr1[i] > max)
max = arr1[i];
else
a = true;
}
These 2 lines:
int CAPACITY1 = 5;
int arr1[CAPACITY1];
mean that the size of your array arr1 is 5, that is, it has 5 elements, from 0 to 4. Trying to access the element at position 5 is wrong.
That said, in your cout messages you talk about 6 numbers. If you want to store 6 elements, change the value of CAPACITY1 to 6, like:
const int CAPACITY1 = 6;
By your comments I think you are getting confused with the size of the array, possibly because you have heard something like "The index of the last element is always off by 1". Yes it is, but it doesn't mean that to store 6 elements you should use the number 5 when you define it! If you want 6 elements, the size must be 6, there is no trap here. The tricky part is that the index will start from 0, and therefore the last element is at position [size - 1], not at [size]. To clarify:
If you declare int arr1[5]; you will have 5 elements, with the index ranging from 0 to 4.
If you declare int arr1[6]; you will have 6 elements, with the index ranging from 0 to 5.
By the way, this shows why it would be a good idea to avoid hard-coded numbers (6, in this case) and always use variables. Try with
cout << "Enter " << CAPACITY1 << " numbers: " << endl;
This way, the size of the array and the messages will always match.
To summarize, if you want to work with 6 numbers, you need to use
const int CAPACITY1 = 6;
and, since you deal with the first element (at index 0) before the loop, the loop must start at 1 and run until the last element, which has index 5, so:
for (int i = 1; i < CAPACITY1; i++) {
On a side note, your logic to check whether the array is in ascending order is confusing. For you, a == true means it is not ascending. It works, but it is counter-intuitive. I would suggest to change it.