So the code is really simple, its just a main(), but there is something wrong in the if/else statement in the while cycle and I dont't know what it is, I thought this is how it supposed to work, but clearly its not.
The code is creating a 11-element array, but the 0th element of the array is typed in by the user. So for example I type in 5, the array have the numbers from 5 to 15. Then the program shows you the numbers in the array. Then you can type in any numbers, and if your number is equal to any of the numbers in the array, then the program should say: "YEES!!!".
The problem is, the program always says, what it should only if the input number is not equal to any number in the array...
So can please someone explain me why the if/else statement is failing?
I also wrote this in Code::Blocks if that changes something...
The code:
#include <iostream>
using namespace std;
int main(){
int numbers[11];
int input;
cout << "Type in a number: ";
cin >> input;
for (int i=0; i<11; i++){
numbers[i] = input +i;
}
for (int i=0; i<11; i++){
cout << numbers[i] <<endl;
}
while (true){
cout<<endl;
cout << "Type in a number:" <<endl;
cin.sync();
cin >> input;
if (input <= numbers[11] && input >= numbers[0])
cout << "YEES!!!" << endl;
else{
cout << "Number is out of range!" <<endl;
cout << "Please try again!" <<endl;
}
}
return 0;
}
Indexing starts with zero, so if you create an array with a size of N last index always will be N-1. In your case, the index of the last element is 10.
if (input <= numbers[10] && input >= numbers[0]) // accurate
The last element in your array should be 10, not 11 because you start at zero. Try doing
if (input <= numbers[10] && input >= numbers[0])
While the other answers clearly handle the indexing issue (array indexes start at 0 and the last index of an 11 element array is 10) there is a little bit of an XY problem happening here.
If you need to determine if an input number is within the range from 1 to 11, there is absolutely no need to use an array at all. You simply need to check that it's less than or equal to 11 and greater than or equal to 1.
if (input <= 11 && input >= 1) {
// ...
}
Your code is essentially trying to do this but storing the bottom of the range in numbers[0] and the top of the range in numbers[10] with the rest of the array being unused. If you want you could use two variables to store these limits.
Related
I wanted to write a code where user will give input the element of the array and then the elements will be print as an array. Here is my code but the code do not give the array as output.
#include<iostream>
using namespace std;
int main(){
int arr[100] , size , i , num ; //Here we are defining the maximum size of array is 100. So user can choose the size of array by him/her but cannot choose more than 100
cout << "Enter the size of array (Maximum array size you can take is 100) : ";
cin >> size;
if (size > 100)
{
cout << "You cannot take the size more than 100" << endl;
}else{
cout << "Inter the elements using space : ";
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
cout << "Enter data you want to insert : ";
cin >> num;
for (int i = size - 1 ; i >= 0 ; i--)
{
arr[i+1] = arr[i];
}
arr[0] = num;
size++;
}
cout << arr[i] << endl;
return 0;
}
Your question isn't entirely clear, but I see two basic problems.
First, you define variable i at the top of your code. That's fine, although there are arguments for variable names being longer than a single character. Think about searching for uses of that variable -- you're going to get it in all sorts of places that have nothing to do with the variable. while has an i. if has an i. Be that as it may.
But here's a real problem. You have some for loops like this:
for (int i = 0; ....)
There's nothing wrong with that, not exactly. It works. HOWEVER, it's considered bad form to reuse a variable inside an inner block that matches a variable from an outer block. It's legal, but it's a common source of bugs. I recommend you don't do it.
Then, at the bottom, you do this:
cout << arr[i] << endl;
At this point, we're back to the original variable i that you declare at the top. But you never actually initialize it, so it's some random value. And you're not doing any sort of loop.
I suspect if you wrap this inside another of your for-loops, you'd get the results you want.
And get rid of declaring i at the top.
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.
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.
The assignment requires three functions. The user enters digits 0-9 until they enter a 10, which stops input, and counts each number, then outputs how many of each number has been counted. It should only output if the user entered a number for it.
My only problem is that for every element in the array that the user doesn't use, Xcode counts it as a 0, so the final output has an abnormally large amount of zeros. Everything else works fine.
here is my code
#include <iostream>
using namespace std;
// counter function prototype
void count(int[], int, int []);
// print function prototype
void print(int []);
int main()
{
// define variables and initialize arrays
const int SIZE=100;
int numbers[SIZE], counter[10], input;
// for loop to set all counter elements to 0
for (int assign = 0; assign < 10; assign++)
{
counter[assign]=0;
}
// for loop to collect data
for (int index=0 ; input != 10 ; index++)
{
cout << "Enter a number 0-9, or 10 to terminate: ";
cin >> input;
// while loop to ensure input is 0-10
while (input < 0 || input > 10)
{
cout << "Invalid, please enter 0-9 or 10 to terminate: ";
cin >> input;
}
// if statements to sort input
if (input >= 0 && input <=9)
{
numbers[index] = input;
}
}
// call count function
count(numbers, SIZE, counter);
// call print function
print(counter);
return 0;
}
// counter function
void count(int numbers[], int SIZE, int counter[])
{
// for loop of counter
for (int index = 0 ; index < 10 ; index++)
{
// for loop of numbers
for (int tracker=0 ; tracker < SIZE ; tracker++)
{
// if statement to count each number
if (index == numbers[tracker])
{
counter[index]++;
}
}
}
return;
}
// print function
void print(int counter[])
{
// for loop to print each element
for (int index=0 ; index < 10 ; index++)
{
// if statement to only print numbers that were entered
if (counter[index] > 0)
{
cout << "You entered " << counter[index] << ", " << index << "(s)" << endl;
}
}
return;
}
What you're referring to as "XCode count[ing] as a 0" is actually just the uninitialized value. Given that you've decided to restrict the user's input to 0-9, an easy way of solving this dilemma would be, immediately after you size the array, to iterate through the array and set each value to -1.
Thereafter, when the user finishes their input, instead of just couting every single value, only print it with a conditional like the following:
if (counter[index] != -1)
{
cout << "You entered " << counter[index] << ", " << index << "(s)" << endl;
}
Note that this is the kind of use case that's much better suited to something like a linked list or a vector. As it stands, you're not doing anything to resize the array, or guard against overflow, so if the user attempts to enter more than 100 numbers, you'll run into serious problems.
First off, this isn't an answer to your exact question, but rather a suggestion on how to write your code in a much simpler form.
I'm not going to write this for you, as it's an assignment, and a rather simple one. Looks like you have a good handle on things as far as coding goes.
Consider this:
You need to allow the user to enter 0-10, and count all 0-9's. An array has indices, and a integer of array 10, would hold those 10 numbers you're counting by the indices. Now you just have some empty ints sitting around, so why not use them to count?
A code hint:
++numbers[input];
Second hint: Don't forget to initialize everything to zero.
I have been working on this all day but just cant get the output right.
What I want to do is input two numbers, which would be pushed into two arrays so we can subtract or add them and display the result. Seems simple, but there are few catches
Input must be pushed into the array, from the user, one by one.
In case I don't enter a value, code should assume it to be '0' or 'null'. '0' if its in the beginning and 'null' if its in the end. for example if 1st number is 234 and second number is 23 then code should make it into '023' and if I enter first number as 2, 2nd number as 3 but don't enter anything in the end then code should assume it to be null.
Problems
I cant take 'carry' to the next set, in case the sum is greater than 10. Which means the value I m getting is just addition of two numbers doesn't matter if its greater than 10 or not. for example addition of 234 and 890 is giving me [10, 12, 4]
Here is the code.....
#include<iostream>
using namespace std;
main() {
int first[10], second[10], result[10], c, n;
cout << "Enter the number of elements in the array ";
cin >> n;
if (n > 10 || n < 0) {
std::cout << "invalid number, you are a bad reader" << endl;
system("PAUSE");
return 0;
}
cout << "Enter elements of first array " << endl;
for (c = 0; c < n; c++) {
cin >> first[c];
if (first[c] > 9 || first[c] < 0) {
std::cout << "invalid number, you are a bad reader" << endl;
system("PAUSE");
return 0;
}
}
cout << "Enter elements of second array " << endl;
for (c = 0; c < n; c++)
cin >> second[c];
cout << "Sum of elements of two arrays " << endl;
for (c = 0; c < n; c++)
cout << first[c] + second[c] << endl;
if ((first[c] + second[c]) > 9) {
cout << "overflow" << endl;
}
//result[c] = first[c] + second [c];
//cout << result[c] <<endl;
system("PAUSE");
return 0;
}
I would really appreciate some suggestions.
In case your intention is to have the result of e.g.
234 + 890 = 1124
then your summation loop should be in reverse order.
(Since you are reading number of elements of the array from the prompt, you may use this information to input first/second numbers into each array in the order preferred for the following summation loop.)
For the carry problem, you need to setup a variable and use it in the loop like this, for example.
int sum[10] = {0};
int c;
int carry = 0;
for (c = 0; c < n; c++)
{
sum[c] = first[c] + second[c] + carry;
carry = sum[c] / 10;
}
if (c < n)
sum[c] = carry;
else
cout << "overflow";
Use std::vector and learn how to use reverse iterators. So if someone enters 234 you push_back(2), push_back(3), push_back(4) and have [0]=2,[1]=3,[2]=4. Then if the next number is 23 you have [0]=2,[1]=3. Now walk both vector with reverse iterators so the first call to rbegin() will give a pointer to [2]=4 and the other vector will give [1]=3. Now add and carry using push_back into a third vector to store the result. Output the result using reverse iterators to print the result.
This looks like homework, so no sample code.