Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views5 pages

Input C++

The document contains multiple C++ code examples demonstrating how to read input data in various formats, including size and elements, without predefined sizes, and handling comma-separated values. Each case illustrates different methods for storing input in a vector and processing it accordingly. The examples emphasize reading from standard input and converting string representations of numbers into integers.

Uploaded by

Arpan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Input C++

The document contains multiple C++ code examples demonstrating how to read input data in various formats, including size and elements, without predefined sizes, and handling comma-separated values. Each case illustrates different methods for storing input in a vector and processing it accordingly. The examples emphasize reading from standard input and converting string representations of numbers into integers.

Uploaded by

Arpan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

example input

4->size
1 2 3 4 -->elements

code

// Online C++ compiler to run C++ program online


#include <iostream>
#include<vector>
using namespace std;

int main() {
// Write C++ code here

//size given 4
//input given 1 2 3 4
int size;
cin>>size;

vector<int>v(size);
for(int i=0;i<size;i++){
cin>>v[i];
}
std::cout << "Try programiz.pro";

return 0;
}

------------------------------------------------------------

2. example input

no size and array input given

1 2 3 4 5 6

case 1: data type known ---

// Online C++ compiler to run C++ program online


#include <iostream>
#include<vector>
using namespace std;

int main() {
// Write C++ code here

// no size only input given 1 2 3 4


int num;

vector<int>ans;
while(cin>>num){
ans.push_back(num);
}
std::cout << "Try programiz.pro";

return 0;
}

case 2: data type not know (works at every case)

int main() {
// Write C++ code here

// no size only input given 1 2 3 4


string line;
getline(cin,line);

vector<int>arr;
stringstream ss(line);

int num;
while(ss>>num){
arr.push_back(num);
}

return 0;
}

------------------------------------------------------------

3. input given [1,2,3,4,567,3]

1<=a[i]<=4 digit

code :

int main() {
// Write C++ code here

//only input given [1,2,3,4]


string line;
getline(cin,line);

vector<int>arr;
string numstr="";
for(int i=0;i<line.size();i++){
if(isdigit(line[i])){
numstr+=line[i];
}else if(!numstr.empty()){
arr.push_back(stoi(numstr));
numstr="";
}
}

if(!numstr.empty()){
arr.push_back(stoi(numstr));
}

for(int val:arr){
cout<<val<<" ";
}

return 0;
}

---------------------------------------------------------

4. input given 4 1 3 4 5 where first is size ,then elements

// Online C++ compiler to run C++ program online


#include <iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;

int main() {
// Write C++ code here

//only input given 4 1 2 3


string line;
getline(cin,line);

stringstream ss(line);
vector<int>arr;
int size=0;

int num;
int index=0;
while(ss>>num){
if(index==0){
size=num;
index++;
}else{
arr.push_back(num);
}
}

for(int num:arr){
cout<<num<<" ";
}
cout<<size;

return 0;
}

---------------------------------------------------------------

5. example input 2,1 6 where size, elements


code
int main() {
// Write C++ code here

//only input given 4,1 2 3 4


string line;
getline(cin,line);

vector<int>arr;
int size=0;

int num;
int index=0;

string numstr="";
for(int i=0;i<line.size();i++){
if(isdigit(line[i])){
numstr+=line[i];
}else if(!numstr.empty()){
if(index==0){
size=stoi(numstr);
index++;

}else{
arr.push_back(stoi(numstr));
}
numstr="";

}
}

if(!numstr.empty()){
arr.push_back(stoi(numstr));
}

for(int num:arr){
cout<<num<<" ";
}
cout<<size;

return 0;
}

-------------------------------------------------------------

6. example input- 1,2,3,4,67,78,4 7 comma separated are elements and last


digit is size

int main() {
// Write C++ code here

//only input given 2,5,6,7,8 9


string line;
getline(cin,line);

vector<int>arr;
int size=0;

int num;
int index=0;

string numstr="";
for(int i=0;i<line.size();i++){
if(isdigit(line[i])){
numstr+=line[i];
}else if(!numstr.empty()){
arr.push_back(stoi(numstr));
numstr="";

}
}

if(!numstr.empty()){
size=stoi(numstr);
}

for(int num:arr){
cout<<num<<" ";
}
cout<<size;

return 0;
}

You might also like