trying online programming first time factorial giving wrong answer - c++

This is a factorial program,I made on a coding site.Kindly help me find why its wrong?
#include <iostream>
using namespace std;
int main()
{
int i, j, f = 1;
int t, a[100];
cin >> t;//enter test cases
for (i = 0; i < t; i++)
cin >> a[i];//enter all the cases one by one
for (i = 0; i < t; i++)
{
for (j = 1; j <= a[i]; j++)
f = f*j;
cout << f;//displays each factorial
}
return 0;
}

You should reinitialize the value of f back to 1 at the start of each test case.
#include <iostream>
using namespace std;
int main()
{
int i, j, f = 1;
int t, a[100];
cin >> t;//enter test cases
for (i = 0; i < t; i++)
cin >> a[i];//enter all the cases one by one
for (i = 0; i < t; i++)
{
f = 1 // add this line
for (j = 1; j <= a[i]; j++)
f = f*j;
cout << f;//displays each factorial
}
return 0;
}

You should reinitialize the value for your factorial variable f each time you are computing the factorial, also you need not use array to store all the asked factorial cases unnecessarily waste of memory.
Here is the optimized solution:
#include <iostream>
using namespace std;
int main()
{
int i, j, f,num;
int t;
cin >> t; //enter test cases
for (i = 0; i < t; i++)
{
cin >> num;
f = 1;
for (j = 1; j <= num; j++) {
f = f * j;
}
//displays each factorial
cout << f << endl;
}
return 0;
}

Related

A number K and an Array of size N is given, check whether we can get the sum of any two elements of array equal to K

I tried to solve the problem but my code still contains some bugs. Why isn't it running?
Here is the link of the question website: https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/practice-problems/algorithm/pair-sums/?
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int n = 1e7 + 10;
int hsh[n];
int main()
{
int n, k;
cin >> n >> k;
int A[n];
for (int i = 0; i < n; i++)
{
cin >> A[i];
}
for (int i = 0; i < n; i++)
{
hsh[A[i]] = k - A[i];
}
int t = 0;
for (int i = 0; i < n; i++)
{
if (hsh[A[i]] == k - hsh[hsh[A[i]]])
{
cout << "YES";
t = 1;
break;
}
}
if (t == 0)
{
cout << "NO";
}
return 0;
}
The problem is that while hsh[A[i]] is always valid, hsh[hsh[A[i]] is not.
Consider the following input:
1 1
10000
This does the following:
A[0] = 10000;
...
hsh[10000] = 1 - 10000; // = -99999
...
if (hsh[10000] == 1 - hsh[-99999]) {...}
So your code is reading out of bounds of the array hsh[]. Make sure you check first if hsh[A[i]] >= 0.
Note that your code is more complicated than necessary; you can do a single loop over the input to check if there is a matching pair:
#include <iostream>
static constexpr int max_k = 2e6;
static bool seen[max_k + 1];
int main()
{
int n, k;
std::cin >> n >> k;
for (int i = 0; i < n; ++i)
{
int A;
std::cin >> A;
if (A <= k && seen[k - A]) {
std::cout << "YES\n";
return 0;
}
seen[A] = true;
}
std::cout << "NO\n";
}

I was trying to run half nested loops and try doing kind of a half sorting of an array but it doesn't seem to work can you tell me why its not working

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n, a[n];
cin >> n;
float b = n / 2;
for (int i = 1; i <= n; i++)
cin >> a[i];
// the code seems to be not working from here till the point marked below
if (n % 2 == 0)
{
for (int k = 1; k <= b; k++)
{
for (int j = (b + 1); j <= n; j++)
{
if (a[k] > a[j])
swap(a[k], a[j]);
}
}
}
// till this point(pls tell me what's wrong with this part of the code)
for (int i = 1; i <= n; i++)
cout << a[i] << " ";
getch();
return 0;
}

How do I resolve sigabrt error on codechef ide

Sigabrt runtime error occurs of a fatal error, because of an assert statement not returning true? Or use of excessive memory, I'm not able to figure out what I'm doing wrong here, help me out?
( problem 1343 C on codeforces) link
so here's the code.
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
long int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while (i<n && check(i,a)== s) {
if (a[i] > max)max = a[i];
i++;
}
b.push_back(max);
}
int s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
cout << s << endl;
}
}
I have debugged your code and also the modified code has been accepted for the above question.
Mistakes you made:
1. In the below loop, value at i'th index of vector<int> b is being added to long int s. Instead, b[k] should be added to long int s because the variable being used in the loop is k not i.
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
2. In the question, range of variable n is given as (1 ≤ n ≤ 2.10^5). So, it is safe to use int n instead of long int n. Also, when I submitted my code on codeforces it gave me signed integer overflow error when I used long int n.
3. You need to use long long s instead of long int s because the value of each element of array A lies between (−10^9 ≤ a[i] ≤ 10^9 , ai ≠ 0) and when we add the elements it can easily surpass int and long int ranges.
4. Although, the answer got accepted when I used vector<int> a in the function
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
But as the user Scheff has said and is correct that it comes with a penalty in space and time, you should use call by reference i.e. vector<int> &a.
Modified Code:
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i, vector<int> &a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while ((i<n) && (check(i,a)== s)) {
if (a[i] > max)
max = a[i];
i++;
}
b.push_back(max);
}
long long s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[k];
}
cout << s << endl;
}
}
Screenshot of Accepted Answer:

Replace element in array by average

I have a question about the exercise from my course:
Write a program that takes array of real numbers as parameter and replaces each element that is smaller than average of the first and last element, by this average. This is my code:
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i; i < 6; i++) {
cout << "Enter the numbers" << endl;
cin >> arr[i];
}
int f = arr[0];
int l = arr[n - 1];
double av = f + l / 2;
for (int i; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n;
int arr[n];
replaverage(arr, n);
cout << arr << " " << endl;
return 0;
}
Code is working, however as an output, it is giving some kind of address "0x7fff2306a5c0". I'm beginner so I apologize, maybe my error is stupid but I don't know how to fix it. Thanks for helping!
#include <iostream>
#include <string>
using namespace std;
void replaverage(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << "Enter the number: ";
cin >> arr[i];
cout << endl;
}
int f = arr[0];
int l = arr[n - 1];
double av = (f + l) / 2;
for (int i = 0; i < n; i++) {
if (arr[i] < av) {
arr[i] = av;
}
}
}
int main()
{
int n = 6; // Making 6 since you had it hardcoded
int arr[n];
replaverage(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
return 0;
}
First problem: Initialize your loop counters to 0;
Second problem: Initialize n in main being passed as parameter to
something
Third problem: Your average calculation is incorrect. It should be (f+l) / 2. Otherwise it will be doing l/2 + f, which is incorrect.
Fourth problem: You need to loop over your array to see all the
elements

I want to print the following pattern

1
3 2
6 5 4
10 9 8 7
I want to print the following pattern. I have tried very hard but couldn't make the code for it. I have tried everything which came up to my mind.
#include<iostream>
using namespace std;
int main() {
int i, j, n;
cin >> n;
int k = 0;
for (i = 1;i <= n; i++) {
for (j = 1; j <= i; j++) {
k++;
printf("%d ", k);
}
printf("\n");
}
}
the other code which i tried is this.
#include<iostream>
using namespace std;
int main() {
int i, j, n;
cin >> n;
int k = 0;
for (i = 1; i <= n; i++) {
for (j = i; j >= 1; j--) {
k++;
printf("%d ",j);
}
printf("\n");
}
}
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int previousRow = 0;
for(int row = 1; row <= 4; row++)
{
int rowTracker = row;
for(int col = 0; col < row; col++)
{
cout<<rowTracker - col + previousRow<<" ";
}
previousRow += row;
cout<<endl;
}
return 0;
}
#include<iostream>
void printPattern(unsigned numlevels)
{
unsigned last_num = 1;
for(unsigned i = 0; i < numlevels; ++i)
{
unsigned next_num = i + last_num;
for(unsigned j = next_num; j >= last_num; --j)
{
std::cout << j << ' ';
}
std::cout << '\n';
last_num = next_num + 1;
}
}
int main()
{
unsigned n;
std::cin >> n;
printPattern(n);
return 0;
}
You may also use a stack to implement this. Here is a working answer:
#include <iostream>
#include <stack>
using namespace std;
int main() {
int i, j, n;
stack<int> st;
cin >> n;
int k = 0;
for(i = 1;i <= n; i++) {
for(j = 1; j <= i; j++) {
k++;
st.push(k);
}
while(!st.empty()){
printf("%d ", st.top());
st.pop();
}
printf("\n");
}
}
Hope it helps!
Before reading the code blow, you should really try to do it yourself. This problem is obviously for practice and to develop the programming muscle. Just getting the answer is not going to help.
The issue with your code is that for each row, the range you want to print is not being determined correctly. You should first find the range and then print the numbers. Ther can be multiple approaches to it. Below is one of them.
for(i=1;i<=n;i++){
int max = i*(i+1)/2;
int min = i*(i-1)/2 + 1;
for(j=max;j>=min;j--){
printf("%d ",j);
}
printf("\n");
}
Here is a simple method
int main(int argc, char* argv[])
{
int n = 4; // suppose print 4 lines
for (int i = 1; i <= n; ++i)
{
int i0 = (i + 1) * i / 2; // first number of line i
for (int j = 0; j < i; j++)
cout << i0 - j << " ";
cout << endl;
}
return 0;
}
thanx everyone for your responses. I was able to do it on my own. Below is what I did. if there is any correction let me know
#include<iostream>
using namespace std;
int main(){
int i,j,n,temp;
cin>>n;
int k=0;
for(i=1;i<=n;i++){
k=k+i,temp=k;
for(j=1;j<=i;j++){
cout<<temp<<+" ";
temp--;
}
cout<<("\n");
}
}