Sum of digits of each elements inside an array of integers - c++

I want to calculate the sum of digits in each elements in array. The problem is with this code it only calculates the the sum of odd indexes (1,3,5...) in array. And in console it shows some random numbers for even indexes (0,2,4...)
Can anybody tell me what is the problem?
And yes I need to use it as array
Here are output values:
Enter how many numbers you want to calculate sum of digits: 5
Enter those numbers: 12
Enter those numbers: 33
Enter those numbers: 44
Enter those numbers: 22
Enter those numbers: 33
Sum of 0 number is: 4
Sum of 1 number is: 6
Sum of 2 number is: 40
Sum of 3 number is: 4
Sum of 4 number is: 11730950
#include <iostream>
int main(int argc, char** argv)
{
int n;
int temp;
int pom;
cout << "Enter how many numbers you want to calculate sum of digits: ";
cin >> n;
int numbers[n];
int sum[n];
for (int i = 0; i < n; i++)
{
cout << "Enter those numbers: ";
cin >> numbers[i];
}
for (int i = 0; i < n; i++)
{
while (numbers[i] > 0)
{
temp = numbers[i] % 10;
sum[i]+= temp;
numbers[i] = numbers[i]/10;
}
}
for (int i = 0; i < n; i++)
{
cout << "Sum of " << i << " number is: " << sum[i] << endl;
}
return 0;
}

You need to initialize the sum array, like this:
int sum[n] {};
otherwise, the first time you read from an element of sum you have undefined behaviour.
Also, variable length arrays are not part of standard c++. If you don't know the size of the array at compile time, just use a std::vector.
If you absolutely must use an array, then you will need to dynamically allocate it, like this:
int * arr = new int[n]{};

#include <iostream>
using namespace std;
int main() {
int a,temp,sum=0;
cin>>a;
int arr[a];
for(int i=0;i<a;i++)
{
cin>>arr[i];
}
for(int i=0;i<a;i++)
{
sum=0;
while(arr[i]>0)
{
temp=arr[i]%10;
sum+=temp;
arr[i]=arr[i]/10;
}
cout<<sum<<" ";
}
}

Related

C++ Write a function to input a list of numbers and print the frequency of the first input number

I am trying to write a C++ program to ask user to input a list of 5 numbers and print out the counts of the first number in the input. I have been trying to use array[] but I have some problems. The ideal inputs and outputs are :
Input : 1 1 2 3 1 Output: 3 because there are 3 counts of 1
Input : 1 2 3 4 5 Output: 1
Input : 1 1 1 0 0 Output: 3
Here are my codes, my code allows me to take the inputs but it does not do anything with it. Any help is appreciated!
#include <iostream>
using namespace std;
//frequency function
int frequency(int a[])
{
int count = 0;
for (int i=0; i < 6; i++)
if (a[i] == a[0])
{
count++;
}
cout << count << endl ;
return count;
}
// Driver program
int main() {
int i;
cout << "Please enter your numbers: ";
int a[5] = {a[0],a[1],a[2],a[3],a[4]};
for (i = 1; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[0] >> a[1] >> a[2]>> a[3] >> a[4];
return 0;
}
int n = sizeof(a)/sizeof(a[0]);
cout << frequency(a);
}
I tried another simpler approach but it needs a little more help
#include <iostream>
#include <string>
using namespace std ;
int main(){
cout << "Please enter your numbers: ";
int a[5];
int repeat;
int first = a[0];
int i;
for (i = 0; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
if (a[i] == first){
repeat++;
}
cout << "Count: " << repeat;
}
you have an odd mixture of techniques for reading a set of numbers. You simply need
cout << "Please enter your numbers: ";
int a[5];
for (int i = 0; i < 5; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
and you certainly dont want that return as it will end the program
you would be better off using std::vector then you would not need to hard code the array size.
Note at the moment you have 6 as the arrays size in 'frequency' but 5 in main

Sum of long integer array in c++

I know this question asked many times but I am facing different problem in my code, I try to calculate sum of long integers range between 2-15.
Code:
long array[20];
long NUMBERS;
cout << "How many numbers ? : ";
cin >> NUMBERS;
long sum=0;
for (int i = 0; i < NUMBERS;i++){
cout << "Input number " << (i+1) << " : ";
cin >> array[i];
}
cout << "Calculate Sum" << endl;
for (int i = 0; i < NUMBERS;i++){
sum = sum + array[i];
}
cout << "Sum is : " << sum << endl;
When I input these three numbers.
1234567
123456
12345
Output:
Sum is : 1370368
but actual answer is : 3703627.
I try these solutions
summing-large-numbers and sum-of-alternate-elements-of-integer-array but still not get right solution, also how we can solve this problem if user input different number with different ranges.
This isn't about programming, but math...
Hope this helps: http://www.wikihow.com/Add-Large-Numbers
(As a simple example, add 1 and 11. What is the result? 12 or 21?)
my code sum large number with string.
first you enter number of numbers you want to sum(to 25).
and then you enter the number (to 180 'each number').
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (){
int band;
cin >> band;
string string_of_number[25];
for (int i=0; i<band; i++){ //for get all string number
cin >> string_of_number[i];
}
int strings_length[band];
for (int i=0; i<band; i++){ //for get all length of strings
strings_length[i]=string_of_number[i].length();
}
int answer[180];
for(int i=0; i<180; i++){
answer[i]=0;
}
int remaner=0;
int sum=0;
int last=180;
for (int h=0; h<band; h++){ // for sum strings with sum answer one by one
int j=179;
for (int i=strings_length[h]; i>=0; i--){
if(string_of_number[h][i]=='\0'){
i--;
}
sum = (remaner + (string_of_number[h][i]-'0') + answer[j]) %10;
remaner = ((string_of_number[h][i]-'0') + answer[j]) / 10;
answer[j]=sum;
if (i==0 && remaner>0){
j--;
answer[j]+=remaner;
remaner=0;
}
if(last>j)
last=j;
j--;
}
}
for(; last<180; last++){
cout << answer[last];
}
}
It seems that your program assumes all numbers are 7 digit:
1234567
123456[0]
12345[00]

How can I find a missing number in a sequence of numbers?

Numbers should be input as:
53,55,57,58,54
Output:
Missing number is 56
Input should be able to be as many numbers as wanted.
This is what I have so far:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int getMissingNo (int a[], int n)
{
int i, total;
total = (n+1)*(n+2)/2;
for ( i = 0; i< n; i++)
total -= a[i];
return total;
}
int main()
{
int a[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
cout << "Enter number of numbers in sequence: ";
int numInSeq;
cin >> numInSeq;
cout << endl;
cout << "Enter numbers in sequence: ";
for (int i = 0; i < numInSeq; i++)
{
cin >> a[i];
}
cout << endl;
int miss = getMissingNo(a,numInSeq);
cout << "Missing number: " << miss << endl << endl;
return 0;
}
The only thing I am missing is being able to enter the numbers separated by commas and I need to edit getMissingNo so it can be any sequence of numbers not just one that starts with 1.
Simple solution if you know the range.
For a given range, let S be the sum of all the numbers within that range.
For a given array with a missing number, let MS be the sum of the numbers in this array.
The missing number is S - MS

How to count how many times each number has been encountered?

I am trying to write a program to count each number the program has encountered. by putting M as an input for the number of the array elements and Max is for the maximum amount of number like you shouldn't exceed this number when writing an input in the M[i]. for some reason the program works just fine when I enter a small input like
Data input:
10 3
1 2 3 2 3 1 1 1 1 3
Answer:
5 2 3
But when I put a big input like 364 for array elements and 15 for example for max. the output doesn't work as expected and I can't find a reason for that!
#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include<conio.h>
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int checker[1000];
int element_cntr = 0;
int cntr = 0;
int n = 0;
void main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue>> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
checker[i]= M[i] ;
element_cntr++;
if (M[i] > Max)
{
cout << "the element number " << element_cntr << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (M[n] == checker[j])
{
cntr+=1;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
n++;
}
}
You have general algorithm problem and several code issues which make code hardly maintainable, non-readable and confusing. That's why you don't understand why it is not working.
Let's review it step by step.
The actual reason of incorrect output is that you only iterate through the first Max items of array when you need to iterate through the first Max integers. For example, let we have the input:
7 3
1 1 1 1 1 2 3
While the correct answer is: 5 1 1, your program will output 5 5 5, because in output loop it will iterate through the first three items and make output for them:
for (int i = 0; i < Max; i++)
for (int j = 0; j < ArrayValue; j++)
if (M[n] == checker[j]) // M[0] is 1, M[1] is 1 and M[2] is 1
It will output answers for first three items of initial array. In your example, it worked fine because the first three items were 1 2 3.
In order to make it work, you need to change your condition to
if (n == checker[j]) // oh, why do you need variable "n"? you have an "i" loop!
{
cntr += 1;
}
It will work, but both your code and algorithm are absolutely incorrect...
Not that proper solution
You have an unnecessary variable element_cntr - loop variable i will provide the same values. You are duplicating it's value.
Also, in your output loop you create a variable n while you have a loop variable i which does the same. You can safely remove variable n and replace if (M[n] == checker[j]) to if (M[i] == checker[j]).
Moreover, your checker array is a full copy if variable M. Why do you like to duplicate all the values? :)
Your code should look, at least, like this:
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int cntr = 0;
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue >> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
if (M[i] > Max)
{
cout << "the element number " << i << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (i == M[j])
{
cntr ++;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
}
return 0;
}
Proper solution
Why do you need a nested loop at all? You take O(n*m) operations to count the occurences of items. It can be easily counted with O(n) operations.
Just count them while reading:
using namespace std;
int arraySize;
int maxValue;
int counts[1000];
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> arraySize >> maxValue;
int lastReadValue;
for (int i = 0; i < arraySize; i++)
{
cin >> lastReadValue;
if (lastReadValue > maxValue)
cout << "Number " << i << " is bigger than maxValue! Skipping it..." << endl;
else
counts[lastReadValue]++; // read and increase the occurence count
}
for (int i = 0; i <= maxValue; i++)
{
if (counts[i] > 0)
cout << i << " occurences: " << counts[i] << endl; // output existent numbers
}
return 0;
}

Sum of Numbers C++

I am supposed to write a program that asks the user for a positive integer value. The program should use a loop to get the sum of
all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of
1, 2, 3, 4, ... 50.
But for some reason it is not working, i am having trouble with my for loops but this is what i have down so far.
#include <iostream>
using namespace std;
int main()
{
int positiveInteger;
int startingNumber = 1;
int i = 0;
cout << "Please input an integer up to 100." << endl;
cin >> positiveInteger;
for (int i=0; i < positiveInteger; i++)
{
i = startingNumber + 1;
cout << i;
}
return 0;
}
I am just at a loss right now why it isn't working properly.
The loop is great; it's what's inside the loop that's wrong. You need a variable named sum, and at each step, add i+1 to sum. At the end of the loop, sum will have the right value, so print it.
try this:
#include <iostream>
using namespace std;
int main()
{
int positiveInteger;
int startingNumber = 1;
cout << "Please input an integer upto 100." << endl;
cin >> positiveInteger;
int result = 0;
for (int i=startingNumber; i <= positiveInteger; i++)
{
result += i;
cout << result;
}
cout << result;
return 0;
}
I have the following formula that works without loops. I discovered it while trying to find a formula for factorials:
#include <iostream>
using namespace std;
int main() {
unsigned int positiveInteger;
cout << "Please input an integer up to 100." << endl;
cin >> positiveInteger;
cout << (positiveInteger * (positiveInteger + 1)) / 2;
return 0;
}
You can try:
int sum = startingNumber;
for (int i=0; i < positiveInteger; i++) {
sum += i;
}
cout << sum;
But much easier is to note that the sum 1+2+...+n = n*(n+1) / 2, so you do not need a loop at all, just use the formula n*(n+1)/2.
mystycs, you are using the variable i to control your loop, however you are editing the value of i within the loop:
for (int i=0; i < positiveInteger; i++)
{
i = startingNumber + 1;
cout << i;
}
Try this instead:
int sum = 0;
for (int i=0; i < positiveInteger; i++)
{
sum = sum + i;
cout << sum << " " << i;
}
int result = 0;
for (int i=0; i < positiveInteger; i++)
{
result = startingNumber + 1;
cout << result;
}
First, you have two variables of the same name i. This calls for confusion.
Second, you should declare a variable called sum, which is initially zero. Then, in a loop, you should add to it the numbers from 1 upto and including positiveInteger. After that, you should output the sum.
You are just updating the value of i in the loop. The value of i should also be added each time.
It is never a good idea to update the value of i inside the for loop. The for loop index should only be used as a counter. In your case, changing the value of i inside the loop will cause all sorts of confusion.
Create variable total that holds the sum of the numbers up to i.
So
for (int i = 0; i < positiveInteger; i++)
total += i;