This is my first time using online judge, I tried a simple program to become familiar with the environment.
Here is the question.
I solved it as the following, but get a wrong answer!
#include<stdio.h>
#include<math.h>
#include<iostream>
int main()
{
int t;
int n;
int num[10];
int i,j,temp;
int s;
int fmid;
std::cin >>t;
int iter=0;
while (iter<t)
{
std::cin>>n;
if (n!=-1){
for(i=0;i<n;i++)
std::cin>>num[i];
for( i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(num[i]>num[j])
{temp=num[i];
num[i]=num[j];
num[j]=temp;
}
s=0;
if (n%2 ==0)
{
int mid=n/2-1;
int midd=mid+1;
s=(num[mid]+num[midd])/2;
fmid=s;
}
else
{s=ceil(n/2);
fmid=num[s];}
std::cout<<fmid;
}
iter++;
}
return 0;
}
Any suggestion is much appreciated.
Thanks
I would read all the numbers, store them in an array, then sort the array using std::sort in <algorithm>
Please find my code below:
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
int arr[10];
int main()
{
int N;
while ((std::cin >> N) && (N!=-1)){
for(int i=0;i<N;i++) {
std::cin >> arr[i];
}
std::sort(arr,arr+N);
if(N%2 == 1){
std::cout << arr[N/2] << std::endl;
}
else {
double ans = ((double)arr[N/2] + (double)arr[(N/2)-1]) / 2.0;
std::cout << ans << std::endl;
}
}
return 0;
}
I hope it works for you.
May I know the problem number? I will try to submit there and give you an AC solution.
Related
I tried to write a simple code to calculate an array elements' sum. every thing looks normal but the function return the sum value wrongly (it always multiply it by two). Although if I want just print the value, it works fine.
this is the code:
#include <iostream>
using namespace std;
void getElements(int[],int);
int sumOfElements(int[],int);
int number;
int sum=0;
int main()
{
int a[10];
getElements(a,5);
sumOfElements(a,5);
cout<<"The sum is "<<sumOfElements(a,5)<<endl;
return 0;
}
//Getting array's elements
void getElements(int numbers[],int size_)
{
for (int i=0; i<size_; i++)
{
cout<<"numbers["<<i<<"]: ";
cin>>number;
numbers[i]=number;
}
cout<<'\n';
}
//Calculation the sum of array's elements
int sumOfElements(int numbers[],int size_)
{
for(int i=0;i<size_;i++)
{
sum+=numbers[i];
}
cout<<sum<<endl;
return sum;
}
any idea? thank you in advance!
You defined int sum globally and were calling sumOfElementstwice, so sum contained twice what you expected.
Here is a modified version of your code that does what you want:
#include <iostream>
using namespace std;
void getElements(int[], int);
int sumOfElements(int[], int);
int main() {
int numbers[5];
getElements(numbers, 5);
cout << sumOfElements(numbers, 5);
return 0;
}
void getElements(int numbers[], int size) {
for (int i = 0; i < size; i++) {
cin >> numbers[i];
}
}
int sumOfElements(int numbers[], int size) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i];
}
return sum;
}
Here is a modified and simpler version of your program:
#include <array>
#include <iostream>
#include <numeric>
using namespace std;
int main(){
const int num_elements_to_sum = 5;
array<int, num_elements_to_sum> elements;
for(int i=0; i<num_elements_to_sum; ++i){
cin>>elements[i];
}
int sum = accumulate(elements.begin(), elements.end(), 0);
cout<<"Sum: "<<sum<<endl;
return 0;
}
C++ has a dedicated fixed size array container, use this instead of C-style arrays. This then allows to use standard library algorithms instead of your own implementation (e.g. accumulate).
I am doing a code in c++ where I am supposed to be finding the series and I build the function for the series myself yet and I call the function I don't find my answer
here is my code
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
harmonicSeries(n);
}
double harmonicSeries(int n) {
for (int i = 1; i <= n; i++) {
float s;
float sum = 0.0;
s = 1 / n;
sum += s;
return sum;
}
}
I will be thankful for any help
See I have made the changes in your code,this works fine in this finding numbers and adding to get their sum.You should use return outside the function and basically harmonic series is of form 1/n which can be any float number or double number so I use s as double and i has float(which by this).
s=1/i(double=1/float,gets converted to double)
#include <iostream>
#include <cmath>
using namespace std;
double harmonicSeries(int n);
int main() {
int n;
cout << "Enter n" << endl;
cin >> n;
cout<<harmonicSeries(n);
}
double harmonicSeries(int n) {
double sum=0.00;
double s;
for (float i = 1; i <= n; i++) {
s = 1 / i;
sum += s;
}
return sum;
}
If you find anything wrong do ask for sure:)
Here is the link to the problem -> PRIME1
It asks us to print all the prime numbers between two numbers m and n
I used segmented sieve to solve the problem. Stored all the primes till sqrt(10^9) and used them to get primes in a specific range. Please help me out.
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
vector<bool> prime(32022);
void precompute()
{
int n=32000;
prime[0]=prime[1]=true;
for(int i=2;i*i<=32000;i++)
{
for(int j=i*i;j<=32000;j+=i)
prime[j]=true;
}
for(int i=0;i<=32000;i++)
if(!prime[i]) v.push_back(i);
}
int main() {
precompute();
int t; scanf("%d",&t);
while(t--)
{
int m,n; scanf("%d%d",&m,&n);
if(n<=32000)
{
for(int i:v)
{
if(i>n) break;
if(i>=m && i<=n)
printf("%d\n",i);
}
}
else
{
vector<bool> prime(n-m+1,true);
for(int i:v)
{
int st=(m/i)*i;
if(st<m) st+=i;
while(st<=n)
{
prime[st-m]=false;
st+=i;
}
}
for(int i=0;i<n-m+1;i++)
{
if(prime[i]) printf("%d\n",i+m);
}
}
printf("\n");
}
}
I am getting segmentation fault while implementing mergesort.
I have checked for array out of bounds.I would love some help to find out where I have gone wrong.I have tried inputs for small arrays such as of size 10 where I have taken size of temp as static(>10).I have been pulling my hair out for hours.
UPDATE: I only needed to change mid=(low+high)/2.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void merges(int arr[],int low,int mid,int high)
{
int i=low,j=mid+1,k=0;
int temp[high-low+1];
while(i<=mid && j<=high)
{
if(arr[i]<arr[j])
{
temp[k++]=arr[i];
i++;
}
else
{
temp[k++]=arr[j];
j++;
}
}
if(i>mid)
{
while(j<=high)
{
temp[k++]=arr[j];
j++;
}
}
else
{
while(i<=mid)
{
temp[k++]=arr[i];
i++;
}
}
j=0;
for(i=low;i<=high;i++)
{
arr[i]=temp[j++];
}
}
void mergesort(int arr[],int low,int high)
{
if(low<high)
{
int mid=low+high/2;
mergesort(arr,low,mid);
mergesort(arr,mid+1,high);
merges(arr,low,mid,high);
}
}
int main(){
int n;
cin >> n;
int arr[n];
for(int arr_i = 0;arr_i < n;arr_i++){
cin >> arr[arr_i];
}
int i,j,k;
mergesort(arr,0,n-1);
for(i=0;i<n;i++)
cout<<arr[i];
return 0;
}
i only needed to change mid=(low+high)/2. Thank you #Gerado Gálvez for your suggestion
You're recursively calling mergesort. Although your general case seems legitimate, I cannot ascertain your base case (i.e. to exit the recursion).
I was writing a code to sort and output the number by using "pair".
I tried some cases bat there was no output.
How should I rewrite the code?
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
int k, i, n;
cin >> n;
pair<int, int> a[n];
for (i = 0; i < n; i++) {
cin >> k;
a[i].first = -k;
a[i].second = i + 1;
}
sort(a, a + n);
for (i = 0; i++; i < n) {
cout << a[i].second;
}
}
for(i=0;i++;i<n){
You meant to write this as:
for(i=0;i<n;i++){