The task is to read an integer array from a user and then count sort it. I used vector to take input of the array but it only stops reading integers after I enter non-integer symbol when it has to do it after the last integer. For example, if I enter (5 4 3 2 1) it does not proceed further but keeps expecting input from a user. Also the sorted array must look like "1 2 3 4 5) but I have 0 at the beginning of it (0 1 2 3 4 5).
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
int main(){
vector<int> arr;
int x;
int j = 0;
while(cin.good()==1){
cin>>x;
arr.push_back(x);
j++;
}
cin.clear();
int count_array[j]={0};
int s=0;
int new_array[j];
int i;
for(i=0; i<j; i++)
count_array[arr[i]]++;
for(i=0; i<j; i++){
count_array[i] = count_array[i] + s;
s=count_array[i];
}
for(i=0;i<j;i++){
new_array[count_array[arr[i]]]=arr[i];
count_array[arr[i]]--;
}
for(i=1;i<=j;i++)
{
cout<<new_array[i]<<" ";
}
return 0;
}
Related
I'm new to c++ but long story short , i want to write a c++ program that will accept input from user through an array and it will sum each array element input
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main() {
int array[100];
int sum;
for(int i=0; i<100; i++){
cout<<"Insert element "<<i<<": ";
cin>>array[i];
sum = array[i]+ //summ with the next array input;
cout<<sum;
}
return 0;
}
means that if i enter any integer the program should be able to give the summation of the inputs in sequence from the first input to the last input
C++ standard library already has a function to do this which is std::accumulate:
#include <iostream>
#include <numeric>
int main() {
int array[5] = {1,2,3,4,5};
int total = std::accumulate(std::begin(array), std::end(array), 0);
return 0;
}
If you plan to use not a full array you should use std::begin(array), std::begin(array) + amount as ranges.
initialize your sum var to 0 initially and write sum+=array[i] instead of what you have written, there is also a limitation in this program as value of sum after all user input should be <=10^9 as int datatype store no. approximately upto 10^9 so take note of this fact also. Write cout<<sum<<endl; instead to be able to distinguish till previous input sum to the new input sum.
Hope this will help.
You can use a do while loop:
#include<iostream>
#include<string>
#include<math.h>
int main()
{
int array[100];
int sum=0;
int i=0;
std::cout<<"Insert element"<<" "<<i<<": ";
std::cin>>array[i];
do
{
sum=sum+array[i];
std::cout<<sum<<std::endl;
i++;
std::cout<<"Insert element"<<" "<<i<<": ";
}while(std::cin>>array[i]);
return 0;
}
If all you want is the sum then just compute the sum. No need to store anything:
#include <iostream>
#include <string>
#include <math.h>
int main() {
int sum = 0;
for(int i=0; i<100; i++){
std::cout << "Insert element " << i << ": ";
int t;
std::cin >> t;
sum += t
std::cout << sum;
}
}
So I had a question that required me to take input of a list of variable lists of which I was only given the list size and not the lengths of the variable lists inside it.
INPUT:
3
1 5 7 2
3 6 2 6 2 4
6 2 3 5 3
The first line of INPUT is the size of the list of lists, followed by input of each list which are variable in size. How can I take this input inside a vector<vector<int>> in C++?
You could use std::getline() to input each line, then use istringstream or std::stoi to parse the strings into ints.
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
vector <vector<int>> DATA;
int main(){
int N;
cin >> N;
string input;
for(int i = 0; i < N; ++i){
getline(cin, input);
istringstream my_stream(input);
vector <int> curr;
int num;
while(my_stream >> num){
curr.push_back(num);
}
DATA.push_back(curr);
cin.ignore();
}
return 0;
}
You can do the thing with the help of the std::stringstream as shown:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main(void) {
vector<vector<int>> mainVector{};
string tempInput = "";
int number = 0;
int lines = 0;
cout << "Enter the number of lines: ";
cin >> lines;
for (int i = 0; i <= lines; i++) {
// temporary vector
vector<int> tempVector{};
// getting the entire inputted line from the user
getline(cin, tempInput);
// parsing the string into the integer
stringstream ss(tempInput);
// pushing the integer
while (ss >> number)
tempVector.push_back(number);
mainVector.push_back(tempVector);
}
// displaying them back to verify they're successfully stored
for (int i = 0; i <= lines; i++) {
for (size_t j = 0, len = mainVector[i].size(); j < len; j++)
cout << mainVector[i][j] << ' ';
cout << endl;
}
return 0;
}
A sample output:
Enter the number of lines: 3
1 5 7 2
3 6 2 6 2 4
6 2 3 5 3
1 5 7 2 // printing the stored vector
3 6 2 6 2 4
6 2 3 5 3
The problem is to take 3 inputs:
1) No. of test cases
2) No. of digits in a number
3) N space separated digit numbers
And to output :
1) No. of sets
2) No. of combinations in each set
I want to print those outputs but No of combination output is returning zero on each and every set
I've already tried troubleshooting and debugging the problem, but none of those worked....
/* Read input from STDIN. Print your output to STDOUT*/
#include<iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
using namespace std;
int factorial (int count);
int main(int argc, char *a[])
{
//intialize variables
int i,T,b,S[i],N,NN[i],C[i],count=0;
cin >> T;
while(T>0) {
cin >> N;
for(i=0;i<N;i++) {
cin >> NN[i];
if(i<N-1) {
S[i] = (N-i);// S[i] is Category 02
count++;
}//end of if
}//end of for loop
for(int j=0;j<N;j++) {
C[i] = factorial(count)/(factorial(i)*factorial(count - i));//
}//end of for loop
cout <<"No. of sets =" <<count++<<endl;
for(int k=0;k<N;k++) {
cout<<"No.of combinations on each set :";
cout<<C[i]<<endl;
} // end fo for loop
}//end of while loop
return 0;
}//end of main
int factorial(int count)
{
int i;
for(i = count-1; i > 1; i--)
count *= i;
return count ;
}//end of function
THIS OUTPUT IS COMING:
"No. of combinations on set 0 : 0"
"No. of combinations on set 1 : 0"
………….
Well it's gone wrong already here
//intialize variables
int i,T,b,S[i],N,NN[i],C[i],count=0;
What's the value of i here? Answer, it doesn't have one. If i doesn't have a value then what's the size of this array NN[i]? Answer, who knows.
When you declare an array in C++, you must give it a size. The size cannot be a variable, it must be a cosntant. And it especialy cannot be a variable without a value.
Your program has undefined behaviour.
EDIT - this would be an improvement
#include <vector>
int main()
{
int T;
cin >> T;
while (T > 0)
{
int N;
cin >> N;
std::vector<int> NN(N), S(N);
for (int i = 0; i < N; i++)
{
...
First improvement is that I using a std::vector instead of an array. Unlike arrays vectors can have variable sizes. Second improvement is that I only declare variables when I need them, I don't declare all the variables at the start of the function. So I only declare NN and S when I know what the value of N is, so I know how big the vectors need to be.
The goal of my program is to find the number entered by user in an array of integers (array was created automatically), and to show the index of this number (or numbers, if they occurs several times). It works correctly when the desired number occurs only once in array. For example, if there is an array
7 8 0 4 2 7 2
and user entered "8", the output of program will be
Index of the number you entered is: 2
But when we have array:
0 5 3 9 3 7 2
And the user entered "3", the output will be
Index of the number you entered is: 3
And I wonder how to make the program include second "3" number which has index 5. The code of program:
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
int i, N;
int LinearSearch(int Array[], int searchValue)
{
for (i=0; i<N; i++)
{
if (Array[i]==searchValue)
return i;
}
return -1;
}
int main()
{
int searchValue, Array[1000];
cout<<"Size of array: ";
cin>>N;
cout<<"Array: ";
for (i=0; i<N; i++)
{
Array[i]=rand()%10;
cout<<Array[i]<<" ";
}
cout<<"Search value: ";
cin>>searchValue;
if (LinearSearch(Array, searchValue)==1)
cout<<"\nIndex of the number you entered is: "<<LinearSearch(Array, searchValue)+1;
else
cout<<"\nNothing found";
}
You can do it in two ways:
1. Change the LinearSearch's return value to vector, write it like this:
vector<int> LinearSearch(int Array[], int searchValue)
2.Add a vector reference variable in the parameters, it should like this:
int LinearSearch(int Array[], int searchValue, vector<int> &results)
And the the method body in LinearSearch should have little change accordingly.
Because you return from the search function as soon as the value is located:
for (i=0; i<N; i++)
{
if (Array[i]==searchValue)
return i; // <-- as soon as we get here, we break the loop
}
therefore, you will get the first position in which searchValue is located, which is 2 (0-based). Thus, you get 2+1 = 3. To get the last one, you will have to remove the early exit, and keep the current index in a variable, like this:
int LinearSearch(int Array[], int searchValue) {
int index = -1;
for (i = 0; i < N; i++) {
if (Array[i]==searchValue) {
index = i;
}
}
return index;
}
I need to find the biggest sum and it containing numbers and whether the first or second number out of two was bigger. How to find it?
Let's say that n=10, the two put numbers are 6 and 2, followings: 7 and 1,
5 and 6, 1 and 8, 4 and 3. Then the answer should be that the biggest sum is 11, it containing numbers are 5 and 6, and the bigger numb was the second one.
I have a code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
for (i=1; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
for (int j=sum; j<=n/2; j++);
{
cout<<sum<<endl;
}
}
fd.close();
fr<<sum;
fr.close();
return 0;
}
I think your code should be like this :
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
int p, a, i;
int n;
int sum;
ofstream fr("Rezults.txt");
ifstream fd("Data.txt");
fd>>n;
cout<<n<<endl;
fd>>p>>a;
int biggestSum=p+a;
int first = p;
int second = a;
for (i=2; i<=n/2; i++)
{
fd>>p>>a;
sum=p+a;
if(sum > biggestSum)
{
biggestSum = sum;
first = p;
second = a;
}
}
cout <<"biggest sum is "<<biggestSum<<"\n";
cout <<"The first number is "<<first<<"\n";
cout<<"The second number is "<<second<<"\n";
fd.close();
fr<<sum;
fr.close();
return 0;
}
updated : you should be careful to the index i of the for loop it should start by 2 since you read the first two numbers before the for loop.