I am writing a program to sort a user entered array. The following code takes 8 single digit integers to be entered and then put into an array.
#include <iostream>
#include <stdio.h>
using namespace std;
void printArray(int ary[], int n) {
cout << "The sequence you entered is as follows:" << endl;
for (int i=0; i<n; i++) {
cout << ary[i] << " ";
}
cout << "\n";
}
int main() {
using namespace std;
int nums[8];
cout << "Please enter 8 single digits between 1 and 9" << endl;
for(int k=0; k<8; k++) {
scanf("%d", &nums[k]);
}
printArray(nums, 8);
cout << endl;
return 0;
}
But when I input digits, only the first integer is pushed into the array with the rest being 0s.
For example, when the input is:
2,5,7,2,1,4,6,8. The output is: 2 0 0 0 0 0 0 0
The input syntax you should use is like that : 2 5 7 2 1 4 6 8.
or you can change the scanf("%d", &nums[k]); to scanf("%d,", &nums[k]); and now you can enter 2,5,7,2,1,4,6,8
Related
I have this section of code for generating an array of randomly generated and sorted ints, but the nosearch cin quits the program. Calling Cin.ignore() & cin.clear() before and/or after this line does not correct this, no error message is shown, the program just quits as soon as I hit enter on that section. This is the only statement to do this.
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include "search.hpp" //a function called sarch is here, program quits before sarch is called.
using namespace std;
int main () {
srand(time(0));
int tenn;
int minrange;
int maxrange;
cout << "Enter the number of integers: ";
cin >> tenn;
cout << endl << "Enter the lower limit: ";
cin >> minrange;
cout << endl << "Enter the upper limit: ";
cin >> maxrange;
int *arr = new int(tenn*10);
for (int i = 0; i < tenn*10; i++){
*(arr+i) = (rand() % maxrange) + minrange;
}
cout << "list, unsorted: " << endl;
for (int i = 0; i < tenn*10; i++){
cout << *(arr+i) << " ";
}
cout << endl;
for (int i = 0; i < tenn*10; i++){
int low = i;
for (int j = i; j < tenn*10; j++){
if (*(arr+j) < *(arr+low)){
low = j;
}
}
int temp = *(arr+i);
*(arr+i) = *(arr+low);
*(arr+low) = temp;
}
cout << "sorted list: " << endl;
for (int i = 0; i < tenn*10; i++){
cout << *(arr+i) << " ";
}
cout << endl;
int nosearch;
cout << "Enter the number of values you wish to search for: " << endl;
//The next cin will fail
cin >> nosearch;
cout << "v";
//more code here...
I use a makefile to compile the code, and when I run it in terminal, this is what happens:
>./Needle.exe
Enter the number of integers: 2
Enter the lower limit: 0
Enter the upper limit: 10
list, unsorted:
9 4 6 2 4 5 2 9 1 4 7 8 3 4 9 4 6 7 9 9
sorted list:
1 2 2 3 4 4 4 4 4 5 6 6 7 7 8 9 9 9 9 9
Enter the number of values you wish to search for:
3
>
I can only assume something I did above triggered this error, which is why I included so much of the code.
I am trying to write a C++ program to ask user to input a list of 5 numbers and print out the counts of the first number in the input. I have been trying to use array[] but I have some problems. The ideal inputs and outputs are :
Input : 1 1 2 3 1 Output: 3 because there are 3 counts of 1
Input : 1 2 3 4 5 Output: 1
Input : 1 1 1 0 0 Output: 3
Here are my codes, my code allows me to take the inputs but it does not do anything with it. Any help is appreciated!
#include <iostream>
using namespace std;
//frequency function
int frequency(int a[])
{
int count = 0;
for (int i=0; i < 6; i++)
if (a[i] == a[0])
{
count++;
}
cout << count << endl ;
return count;
}
// Driver program
int main() {
int i;
cout << "Please enter your numbers: ";
int a[5] = {a[0],a[1],a[2],a[3],a[4]};
for (i = 1; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[0] >> a[1] >> a[2]>> a[3] >> a[4];
return 0;
}
int n = sizeof(a)/sizeof(a[0]);
cout << frequency(a);
}
I tried another simpler approach but it needs a little more help
#include <iostream>
#include <string>
using namespace std ;
int main(){
cout << "Please enter your numbers: ";
int a[5];
int repeat;
int first = a[0];
int i;
for (i = 0; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
if (a[i] == first){
repeat++;
}
cout << "Count: " << repeat;
}
you have an odd mixture of techniques for reading a set of numbers. You simply need
cout << "Please enter your numbers: ";
int a[5];
for (int i = 0; i < 5; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
and you certainly dont want that return as it will end the program
you would be better off using std::vector then you would not need to hard code the array size.
Note at the moment you have 6 as the arrays size in 'frequency' but 5 in main
I have the following code:
#include <iostream>
using namespace std;
int main()
{
int length = 0;
int arrA[length];
cout << "Enter the length : ";
cin >> length;
cout << "Enter " << length << " integers for array : ";
for(int i = 0; i < length; i++)
{
cin >> arrA[i];
}
cout << "Array A : ";
for(int i = 0; i < length; i++)
{
cout << arrA[i] << " ";
}
cout << endl;
}
The above require the user to input the length of array followed by the integers that will be store in array. It work but the printing of the array value is incorrect when I input length 8 and above.
Working
Enter the length : 7
Enter 7 integers for array : 7 6 5 4 3 2 1
Array A : 7 6 5 4 3 2 1
Not working
Enter the length : 8
Enter 8 integers for array : 8 7 6 5 4 3 2 1
Array A : 8
Could this be due to memory issue or something?
int length = 0;
int arrA[length];
This creates an array of size zero
cout << "Enter the length : ";
cin >> length;
This sets the length variable to a value entered by the user but doesn't change the size of arrA which has already been created.
You obviously think that when the length variable changes that the array arrA will change as well, but this is not true.
Because you are accessing elements of an array which has zero size the behaviour of your program is undefined.
In addition this whole approach is incorrect because
int length;
...
int arrA[length];
is not legal C++. Because length is a variable this is a variable length array or VLA. VLAs are legal in C but are not legal in C++.
The best way to write this code in C++ is to use a vector. A vector is the C++ improvement on a C array.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int length;
cout << "Enter the length : ";
cin >> length;
vector<int> arrA(length);
...
int length = 0;
int arrA[length];
This is not valid C++.
What you may want to do is:
int *arrA = new int[length];
after cin >> length.
You will have to free the pointer later with delete[] arrA;
It is not due to a memory issue. But because variable length arrays are not part of the C++ standard.
length is a variable length array.
Read more about variable length arrays
Here
Here is the code if you do not want to use a vector. I would suggest not to use this for multifarious reasons. But go ahead and knock yourself out.
#include <iostream>
using namespace std;
int main()
{
int length = 0;
int *arrA = new int [length];
cout << "Enter the length : ";
cin >> length;
cout << "Enter " << length << " integers for array : ";
for (int i = 0; i < length; i++)
{
cin >> arrA[i];
}
cout << "Array A : ";
for (int i = 0; i < length; i++)
{
cout << arrA[i] << " ";
}
delete[] arrA;
cout << endl;
return 0;
}
The following code never places anything into the two integer arrays, seqs and seq_sizes but all other variables do contain expected contents when I pause and debug during runtime.
Why would these arrays remain empty despite the calls to place ints into them?
#include <iostream>
using namespace std;
int main(){
int n,q;
cout << "n q:" << endl;
cin>>n>>q;
int m = 1000000;
int seqs[m];
int seq_sizes[n];
cout << "seqs:" << endl;
for (auto i=0;i<n;++i){
cout << i << ":" << endl;
int take;
cin>>take;
seq_sizes[i]=take;
for (auto j=0;j<seq_sizes[i];++j){
int take2;
cin>>take2;
seqs[i+j]=take2;
}
}
cout << "queries:" << endl;
for (auto i=0;i<q;++i){
int seq,index;
cin>>seq>>index;
int tally=0;
for (auto j=0;j<seq;++j){
tally+=seq_sizes[j];
}
cout<<seqs[tally+index]<<endl;
}
return 0;
}
Sample input:
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
for (auto i=0;i<n;++i){
cout << i << ":" << endl;
int take;
cin>>take;
seq_sizes[i]=take;
for (auto j=0;j<seq_sizes[i];++j){
int take2;
cin>>take2;
seqs[i+j]=take2;
}
}
check the functioning of this loop(considering your input)
it sets seq_sizes[0] = 3 once and then it is reinitialized to 1.
Same happens again and again which is causing the error.
Consider changing i+j in line seqs[i+j]=take2;
I built a program that accepts two input from the user, using a array inside a loop, it is passed to a function inside a class which will display the two number.
The problem is when the user is inputting a number and it is 1. The program continuously asks the user to input a number, and when 2 is entered the program ask another number and end, but for example you entered 2 and 3. . . it will then output 2 and 4 (so 3 + 1 ) and always the last number is plus one. Here is the code:
main.cpp:
#include <iostream>
#include "newclass.h"
using namespace std;
int main()
{
int array_variable_main[2];
for(int counter = 1; counter <= 2; counter=counter+1)
{
cout << "Enter a Number: " << endl;
cin >> array_variable_main[counter];
}
newclass sum_object;
sum_object.loop_function(array_variable_main, 2);
return 0;
}
newclass.cpp:
#include "newclass.h"
#include <iostream>
using namespace std;
newclass::newclass()
{
}
void newclass::loop_function(int array_variable[], int arraysize)
{
cout << "The numbers that are stored in the array are: " << endl;
for(int counter = 1; counter <= arraysize; counter = counter+1)
{
cout << array_variable[counter] << endl;
}
}
newclass.h:
#ifndef NEWCLASS_H
#define NEWCLASS_H
class newclass
{
public:
newclass();
void loop_function(int array_variable[], int arraysize);
};
#endif // NEWCLASS_H
You have to remember that array indices go from zero to size-1. So for your array it's zero and one. Anything beyond that leads to undefined behavior. Undefined behavior can't easily be predicted, so the result of your program could be anything.
In C and C++ array index normally starts at 0 so this
int array_variable_main[2];
for(int counter = 1; counter <= 2; counter=counter+1)
{
cout << "Enter a Number: " << endl;
cin >> array_variable_main[counter];
}
will access outside the array
do instead
int array_variable_main[2];
for(int counter = 0; counter < 2; ++counter)
{
cout << "Enter a Number: " << endl;
cin >> array_variable_main[counter];
}