nested For loop pattern in c++ - c++

in c++, when I am making nested For loop and I am trying to calculate the factorial...I don't get the correct factorials...I don't know why. For example factorial of 5 is 120 but here it results in 34560. why?
and here is the code:
int fact=1;
for (int number=1; number<=10; number++) {
for (int i=1; i<=number; i++)
fact=fact*i;
cout <<"factorial of "<<number<<"="<<fact<<"\n";
}
here is it pictured:

You need to re-initialize fact for each number.
int fact=1;
for (int number=1; number<=10; number++) {
fact = 1;
for (int i=1; i<=number; i++)
fact=fact*i;
cout <<"factorial of "<<number<<"="<<fact<<"\n";
}

Related

the program is stuck while giving output

I dont know why the program is failed to give output, This is the code to find number of zero's
in a given array
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
int b=0 , a;
for(int j=0; j<n; j++)
{
a=arr[j];
while(a==0)
{
b=b++;
}
}
cout<<b;
}
Try changing it to
if (a==0) //you had a while here
{
b=b++;
}
Since you are not changing the value of 'a' inside the loop, it gets stuck inside the while and this leads to an infinite loop!
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int b = 0, a = 0;
for (int j = 0; j < n; j++)
{
a = arr[j];
if (a == 0) //here you used while use if as you are checking the condition every iteration of loop.
{
b++; //here instead of using b=b++ use this.
}
}
cout << b;
return 0; //return is optional.
}
just do this in the j for loop. you don't need any a variable or anything
and also runtime variable arrays are not allowed in C++ use dynamic arrays or vectors
for(int j=0; j<n; j++)
{
if(arr[j]==0){
b++;
}
}

How to add two binary numbers in the array c++

I think I made a mistake somewhere but I can't seem to find it. I think the problem lies in adding or entering data in the wrong order. I apologize for any mistakes, English is not my primary language. enter image description here
#include <iostream>
using namespace std;
int main()
{
int d, n, m, carry;
int a[10000];
int b[10000];
int addition[10001];
cin>>d;
for(int i=0; i<d; i++)
{
int a[10000]={0};
int b[10000]={0};
int addition[10001]={0};
cin>>n;
for(int i=n; i>=1; i--)
{
cin>>a[i];
}
cin>>m;
for (int i=m; i>=1; i--)
{
cin>>b[i];
}
if(n<m)
{
n=m;
}
carry=0;
for (int i=1; i<=n; i++)
{
addition[i]=(a[i]+b[i]+carry)%2; //way my teacher
carry=(a[i]+b[i]+carry)/2;
}
addition[n+1]=carry;
// if(addition[n+1]==0)n--;
for(int i=n; i>=0; i--)
{
cout<<addition[i];
}
}
return 0;
}
I think there are at least two errors.
1) In
for(int i=n; i>=0; i--)
you're counting down to 0. But in every other loop, you've counted down to 1. Since you're counting down too far, your output would have an extra zero at the end. (For example, it would show 1000 when it should have shown 100.)
2) Also in that loop, you're starting at n. But you've potentially put a carry into n+1, aren't you forgetting to output it too?

Bubble sort ignoring the first element of the array

I was doing my homework but don't know why bubble sort is not working. it is making first element of the array zero due to some unknown reason.
#include <iostream>
using namespace std;
int main()
{
int *arr,s;
cout<<"Enter the quantity of numbers ";
cin>>s;
arr=new int[s];
for(int i=0;i<s;i++)
{
cout<<"Enter number "<<i+1<<" ";
cin>>*(arr+i);
}
int temp;
for(int j=0;j<s;j++)
{
for(int k=0;k<(s-j);k++)
{
if(*(arr+k)>*(arr+k+1))
{
temp=*(arr+k);
*(arr+k)=*(arr+k+1);
*(arr+k+1)=temp;
}
}
}
for(int x=0;x<s;x++)
{
cout<<*(arr+x)<<"\t";
}
cout<<endl;
return 0;
}
OUTPUT
Enter the quantity of numbers 5
Enter number 1 4
Enter number 2 33
Enter number 3 22
Enter number 4 1
Enter number 5 3
0 1 3 4 22
I don't know why first element is getting zero. and if I run it without bubble sort loops it runs perfectly but don't with those loops.
In this for loop
for(int k=0;k<(s-j);k++)
{
if(*(arr+k)>*(arr+k+1))
^^^^^^^
{
temp=*(arr+k);
*(arr+k)=*(arr+k+1);
*(arr+k+1)=temp;
}
there is an attempt tp access memory beyond the array when j is equal to 0 and k is equal to s - 1. That is in this case k + 1 is equal to s though the valid range of indices is [0, s-1].
At least change the loops the following way
for(int j=0;j<s;j++)
{
for(int k = 1;k<(s-j);k++)
{
if(*(arr+k) < *(arr+k-1))
{
int temp=*(arr+k);
*(arr+k)=*(arr+k-1);
*(arr+k-1)=temp;
}
}
}
Your use of the index is off. You are picking up garbage values from outside your valid range (they just happen to be zeros). An easier style might help detect the problem.
for(int j=0;j<s;j++)
{
for(int k=1;k<s;k++)
{
if(arr[k-1]>arr[k])
{
int temp=arr[k];
arr[k]=arr[k-1];
arr[k-1]=temp;
}
Code
#include <bits/stdc++.h>
using namespace std;
int N, a[10050];
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) scanf("%d" , &a[i]);
for (int k = 0; k < N; k++) {
int mn = k;
for (int i = k+1; i < N; ++i) {
if (a[i] < a[mn]) mn = i;
}
swap(a[k], a[mn]);
}
for (int i = 0; i < N; i++) printf("%d " , a[i]);
}
Explanation
In bubble sort, you swap the elements recursively. Here is a 15-line code. Hope it helps.

Not displaying any output

Not displaying any output for the second loop of the function even though it is displaying the output for the cout statements in between the first and the second loop of the function. Enter 3 4 8 5 for cin. I need the scores to be 0 5 1. And the total score to be 6.
Code:
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;
long getMaxScore(vector < long > a){
// Complete this function
long runningSum = 0;
int n = a.size();
vector<long> scores(n);
int j=0;
long totalScore;
for(int i = 0; i < n;)
{
scores[j] = runningSum%a[n-1];
runningSum = runningSum + a[n-1];
n--;
j++;
}
cout<<scores[0]<<endl;
cout<<scores[1]<<endl;
cout<<scores[2]<<endl;
for(long k=0; k<n; k++)
{
totalScore = 0;
totalScore+=scores[k];
cout<<totalScore<<endl;
}
//return totalScore;}
int main() {
int n;
cin >> n;
vector<long> a(n);
for(int a_i = 0; a_i < n; a_i++){
cin >> a[a_i];
}
long maxScore = getMaxScore(a);
return 0;}
Problem
The loop
for(int i = 0; i < n;)
{
scores[j] = runningSum%a[n-1];
runningSum = runningSum + a[n-1];
n--;
j++;
}
ends when n is equal to zero.
Hence, by the time the program encounters the loop
for(long k=0; k<n; k++)
{
totalScore = 0;
totalScore+=scores[k];
cout<<totalScore<<endl;
}
nothing inside the loop executed since k < n is false.
Solution
I suggest adding a line to reset the value of n before the second loop.
n = a.size();
for(long k=0; k<n; k++)
{
...
}
Move the line
totalScore = 0;
outside the loop to accumulate the score.
totalScore = 0;
for(long k=0; k<n; k++)
{
totalScore += scores[k];
cout << totalScore << endl;
}

How do i output sum of rows of a 2D array C++

EDIT: I'm fairly new to c++. Began working with this language two weeks ago.
Sorry if this have been asked before, but i have searched everywhere on the web on how to sum individual rows in a 2D array and not found the answer that i was looking for.
i need to display the sum of each individual row in a[m][n], but for some reason this only works when my array is 2x2, but if it's 3x3 or bigger, then i get the following output in the terminal:
for intsance, a[3][3]=
{1,2,3}, //this is determined by the user
{1,2,3},
{1,2,3};
then i get the following output:
9179942 //address of some sort???
6 // actual sum. code is working here (yay :D)
469090925// again something i dont understand
This is what i have so far
#include <iostream>
using namespace std;
int main(){
int m,n;
cout<<"Enter number of rows for array"<<endl;
cin>>m;
if (m>10){
cout<<"More than 10 rows will be too big"<<endl;
return 1;
}
cout<<"Enter number of collumns for array"<<endl;
cin>>n;
if (n>10){
cout<<"More than 10 collumns will be too big"<<endl;
return 1;
}
int a[m][n];
for(int i=0; i<m;i++){
cout<<"Enter "<<m<<" values into row "<<i+1<<endl;
for(int j=0; j<n; j++){
cout<<"a ["<<i<<"]["<<j<<"]: ";
cin>>a[i][j];
}
}
cout<<"Array dimensions: "<<m<<"x"<<n<<'\n'<<"resulting array: "<<endl;
for(int i=0; i<m;i++){
for(int j=0; j<n; j++){
cout<<a[i][j]<<" ";
}
cout<<endl;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
avg[i]+=a[i][j];
}
}cout<<"\n\n";
for(int i=0; i<m; i++){
cout<<avg[i]<<endl;
}
return 0;
}
int avg[m];
int el_less_avg;
for(int i=0; i<m; i++){
for(int j=0; j<n;j++){
You're not initializing these values so they're free to be whatever cruft is on the stack at the time. You need to initialize them.
int avg[m];
for (int i = 0; i < m; ++i) {
avg[i] = 0;
for (int j = 0; j < n; ++j) {
avg[i] += a[i][j];
}
}
int a[m][n]; is not allowed in Standard C++. The dimensions of C-style arrays must be known at compile-time. A program using this code could do literally anything.
You could replace this line with:
vector<vector<int>> a(m, vector<int>(n));
which seems like a mouthful at first, but you will find it makes your problem go away.
Another bonus of this approach is that you can then use range-based loops:
for(int x : avg)
cout << x << endl;
which reduces the chance of making an error by using the wrong letter in the loop condition.