How can i use for loop with String Array in c++ - c++

i found errors when compile the mentioned source .
i want to for loop to string array increase with numberer array. like as
1 + st = 1st;
2 + nd = 2nd;
3 + rd = 3rd;
my code is below
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main(){
int value=1;
int ivalue;
int sum=0;
int average;
int x,y;
int count=0;
string A[5]={"st","nd","rd","th","th"};
cout << "Enter loop limit : "; cin >> value ;
cout<<endl;
cout<<endl;
for( x=0;x<=value-1;x++){
for (x=0;x<=A[5];x++)
cout << "Enter "<<x+1<<A[0]<<" value : "; cin >> ivalue;
sum=sum+ivalue;
count++;
}

Does the following code work for you?
int main(){
int value=1;
int ivalue;
int sum=0;
int count=0;
string A[5]={"st","nd","rd","th","th"};
cout << "Enter loop limit : "; cin >> value ;
cout<<endl;
cout<<endl;
for(int x = 0; x <= value - 1; x++){
cout << "Enter "<<x+1<<A[x]<<" value : ";
cin >> ivalue;
sum=sum+ivalue;
count++;
}
cout<<"Sum:"<<sum<<endl;
}

Related

The following code of mine shows segmentation fault . How to prevent it?

The code below enters the number of arrays and the number of queries based on the arrays. The query consists of the array number and the desired element from that particular array.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct array
{
int arr[100000];
int n;
};
int main()
{
int i, j, b, d, e, f;
struct array a[100000];
cout << "Enter the number of arrays and the number of queries:";
cin >> b;
cin >> e;
cout << "Enter the value array size and enter the array elements:";
for(j = 0; j < b; j++)
{
cin >> a[j].n;
int c = a[j].n;
for(i = 0; i < c; i++)
{
cin>>a[j].arr[i];
}
}
for(i=0;i<e;i++)
{
cout << "Enter the query that consists of array number and the index of the desired element:";
cin >> d;
cin >> f;
cout << a[d].arr[f] << "\n";
}
return 0;
}

argument of type “int” incompatible with parameter of type “int*”

The goal is to search the array and find a specific number in it and return the position. I have the method for how to do it down but trying to get it to run in the main method is giving me the error in the title. What do i do to fix?
#include "stdafx.h"
#include <iostream>
using namespace std;
int searchArray(int a[], int x) {
for (int i = 0; i < a[100]; i++) {
if (x == a[i])
return i + 1;
break;
}
return -1;
}
int main()
{
int wait, x, y, a[100];
//problem 3
cout << "Enter the size of the array(1-100): ";
cin >> y;
for (int i = 0; i < y; i++) {
cout << "Enter an array of numbers:";
cin >> a[i];
}
searchArray(a[100], x); //i get error on this line with a[100]
cin >> wait;
return 0;
}
Expected is it should run with no errors and find position of a number in the array but I just get the error and cant run it.
After the for() loop in main(), you need something like:
cout << "Enter the value to search for: ";
cin >> x;
wait = searchArray(a, x);
cout << x;
cout << " is at position: ";
cout << wait;
int searchArray(int a[], int x)
{
for (int i = 0; i < 100; i++) //change a[100] to 100 because it only needs the size not the array itself
{
if (x == a[i])
return i + 1;
break;
}
return -1;
}
int main()
{
int wait, x, y, a[100];
cout << "Enter the size of the array(1-100): ";
cin >> y;
for (int i = 0; i < y; i++) {
cout << "Enter an array of numbers:";
cin >> a[i];
}
cout<<"Number to look for: "; //sets a value for x
cin>>x;
cout<<"Index: "<<searchArray(a, x)<<endl; //function returns an int therefore a cout is needed
cin >> wait;
return 0;
}

c++ variable sized arrays from Hackerrank with vectors

I wanted to solve a challenge named "Variable sized Arrays" on Hackerrank and I wanted to do that using vectors. The Code I wrote, doesn't work properly and I tried debugging it, but I'm getting nowhere. I'll be grateful for
Here's the challenge:
Consider an n-element array,a, where each index i in the array contains a reference Kito an array of integers (where the value of Ki varies from array to array). See the Explanation section below for a diagram.
Given a, you must answer q queries. Each query is in the format i j, where i denotes an index in array and j denotes an index in the array located at a[i] . For each query, find and print the value of element j in the array at location on a[i]a new line.
Input Format
The first line contains two space-separated integers denoting the respective values of n (the number of variable-length arrays) and q (the number of queries).
Each line of the subsequent lines contains a space-separated sequence in the format k a[i]0 a[i]1 … a[i]k-1 describing the k-element array located at a[i].
Each of the q subsequent lines contains two space-separated integers describing the respective values of i (an index in array a) and j (an index in the array referenced by a[i]) for a query.
Sample Input
2 2
3 1 5 4
5 1 2 8 9 3
0 1
1 3
Sample Output
5
9
So here's my code for this (I'll leave it with the couts for debugging):
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::vector;
using std::endl;
int main() {
int numberOfQueries = 0;
int numberOfArrays = 0;
cout << "Enter Nr.Of Arrays followed by Nr.Of Queries:";
cin >> numberOfArrays >> numberOfQueries;
cout << "Nr.Of Arrays: " << numberOfArrays << endl;
cout << "Nr.Of Queries: " << numberOfQueries << endl;
vector<vector<int>>multiArray;
cout << "MultiArray size: " << multiArray.size();
while (numberOfArrays != 0) {
int vsize = 0;
cout << "\nenter array starting by its size: ";
cin >> vsize;
cout << " Size entered is: " << vsize << endl;
vector<int> vec1(vsize);
cout << "Array Size is: " << vec1.size() << endl;
//int element = 0;
while (cin >> vsize) {
cout << "Element is: " << vsize << "\n";
vec1.push_back(vsize);
};
multiArray.push_back(vec1);
numberOfArrays--;
cout << "MultiArray size: " << multiArray.size();
cout << "Nr.Of Arrays: " << numberOfArrays << endl;
};
while (numberOfQueries > 0) {
int i = 0, j = 0;
cout << "\nQuery indexes:";
cin >> i >> j;
cout << multiArray[i][j];
numberOfQueries--;
}
}
while (cin >> vsize) {
cout << "Element is: " << vsize << "\n";
vec1.push_back(vsize);
};
should be something like
for (int i = 0; i < vsize; ++i) {
int elem;
cin >> elem;
cout << "Element is: " << elem << "\n";
vec1.push_back(elem);
}
while (cin >> vsize) isn't going to stop asking for input until you get end of file or an error. But you know how many inputs to expect so code that in a for loop.
My answer.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n,q;
cin >> n >> q ;
vector<int> a[n];
int k;
for(int i = 0; i < n; i++)
{
cin >> k;
for (int j = 0; j < k; j++)
{
int val_a_i_j;
cin >> val_a_i_j;
a[i].push_back(val_a_i_j);
}
};
for (int i = 0; i < q; i++)
{
int a_i, j;
cin >> a_i >> j;
cout << a[a_i][j] << '\n';
}
return 0;
}
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a , b;
cin >> a >> b;
vector<int> arr[a];
for(int i= 0 ; i < a ; i++)
{
int m;
cin >> m;
int o;
for(int j=0;j<m;j++)
{
cin >> o;
arr[i].push_back(o);
}
}
int r,s;
for(int k=1;k<b;k++)
{
cin>>r>>s;
cout <<a[r][s]<< endl;
}
return 0;
}
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n, q;
cin >> n >> q;
vector<int>* a;
a= new vector<int>[n];
int p;
int k;
for (p = 0;p < n;p++) {
cin >> k;
for (int i = 0; i < k; i++) {
int o;
cin >>o;
a[p].push_back(o);
}
}
int r, s;
for (p = 0;p < q;p++)
{
cin >> r >> s;
cout << a[r][s]<<endl;
}
return 0;
}
This problem is about handling vectors of the vector.
int main() {
int n,q;
cin>>n>>q;
vector<vector<int> > a;//creating vectors of vector
int k;
for (int i = 0; i < n; i++)
{
cin >>k;
vector <int> row; // creating vector
for (int j = 0; j < k; j++)
{
int val;
cin>> val;
row.push_back(val);
}
a.push_back(row);
}
for (int l=0; l < q; l++)
{
int i,j;
cin>>i>>j;
// checking boundary conditions
if ((i >= 0 && i < a.size() ) && (j >=0 && j < a[i].size()))
{
cout<<a[i][j]<<endl;
}
}
return 0;
}
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<vector<int>> MultiArry;
vector<int> output;
int n1,n2, q;
int i,j;
cin >> n1 >> q;
MultiArry.resize(n1);
output.resize(q);
for(int i=0;i<n1;i++)
{
cin >> n2;
MultiArry[i].resize(n2);
for(int j=0;j<n2;j++)
{
cin >> MultiArry[i][j];
}
}
for(int ii=0;ii<q;ii++)
{
cin >> i >> j;
output[ii] = MultiArry[i][j];
}
for(int i=0;i<q;i++)
cout << output[i] << endl;
return 0;
}

How to add some numbers inputed by the user given a specific formula

So i need to write a program that asks for some numbers from the user (the amount of numbers is determined by the user) and then add them given this formula: ANSWER = FIRST - SECOND + THIRD - FIFTH + ...
where FIRST, SECOND, etc are the first, second and the rest of the numbers input by the user.
The problem is that i can create a loop that stores the numbers but actually, it only updates the value of the "num" variable. This is the code i have written.
#include <iostream>
using namespace std;
int main() {
int num, counter;
double answer;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
}
return 0;
}
Inserting a if-else clause that controls the remainder of the integer division of index i by 2 you can separate even and odd cases to obtain the desidered effect
#include <iostream>
using namespace std;
int main() {
int num, counter;
double answer;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
if(i%2==0)
answer+=num;
else
answer-=num;
}
return 0;
}
You can also do this assuming you don't need to store the numbers input by the user. What I am basically doing is just toggling between +1 and -1 that I then multiply by the number input by the user and then straightforwardly adding it to the answer.
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num, counter;
double answer = 0;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
answer += num*pow(-1, i);
}
cout<<answer;
return 0;
}
You can also do:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num, counter;
double answer = 0;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
if(i%2 == 0)answer += num;
else answer -= num;
}
cout<<answer;
return 0;
}

Accepting a 2-D array (int type) from a function and returning it to the main function

I just wanna take size of the 2-D array as arguments in the function, accept the array and somehow put it in the main function for further tasks.
I tried watching some videos and posts but did not get my answer.
Please help and take me out of this problem as always.
int *accept(int a,int b)
{
int x[50][50];
for(int i=0;i<a;++i)
{
cout<<"Enter elements for "<<i+1<<" th row";
for(int j=0;j<b;++j)
{
cout<<"Enter "<<j+i<<" th element \n";
cin>>x[i][j];
}
}
return *x;
}
void main()
{
int arr[50][50],m,n;
cout<<"Enter the no of rows you want : ";
cin>>m;
cout<<"Enter the no of columns you want : ";
cin>>n;
arr[50][50]=accept(m,n); //how to copy?
You can use the memcpy(),
#include <iostream>
#include <cstring>
using namespace std;
void accept(void* arr, int a, int b)
{
int x[a][b];
for (int i = 0; i < a; ++i)
{
cout << "Enter elements for " << i+1 << " th row";
for (int j = 0; j < b; ++j)
{
cout << "Enter " << j+i << " th element \n";
cin >> x[i][j];
}
}
memcpy(arr, x, sizeof(int) * a * b);
}
int main()
{
int m, n;
cout << "Enter the no of rows you want : ";
cin >> m;
cout << "Enter the no of columns you want : ";
cin >> n;
int arr[m][n];
accept(&arr, m, n);
return 0;
}