Reputation: 1272
Last week i had a project from my teacher asking me to develop a program which takes in a string (stream of integers to be precise) and calculates the sum of the numbers in the string for each number in the string ie.
if input is 31456
I did submit a working project but it is full of spaghetti code (nested loops which ends if string length is less than the number of loops) which is not a clean approach. I wondered and studied a quite a lot over this situation in vain. I have not found any way of doing this without nested for loops in C (or maybe i gave up too fast ?)
Again, i am not asking you guys for an answer to my problem but wanted to know if there is a way of doing this without the nested loops (which will have problem if length of input > number of nested loops).
Upvotes: 1
Views: 721
Reputation: 1453
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void printResults(int* results,int size);
int main( int argc, char** argv ){
char nums[] = "314156";/* "31456" ? */
int i,tmp;
char tmpc[2];
int length=strlen(nums);
#define LENGTH length
int results[LENGTH];
results[0]=0;
tmpc[0]=nums[0];
sscanf(tmpc, "%d", &tmp);
results[1]=tmp;
int result=results[1];
for(i=2 ; i< length ; ++i){
tmpc[0]=nums[i];
sscanf(tmpc, "%d", &tmp);
result+=tmp;
results[i]=result;
}
printResults(results,length);
return 0;
}
static void printResults(int* results,int size){
int i;
for(i=0;i<size;i++){
printf("%d time loop sum is %d\n", i+1,results[i]);
}
}
This is a really basic and understandable code in C for beginner and the ouput is:
Upvotes: 0
Reputation: 1
#include<iostream>
#include<cctype>
using namespace std;
void main()
{
const int SIZE=10;
char myArray[SIZE];
int length=0,sum=0;
cout<<"enter Array of digit from 1-9\n\n ";
cin>>myArray;
cout<<myArray<<endl;
for(int i=0;i<myArray[i];i++)
{
if(!isspace(myArray[i]))
//if(myArray[i]!=NULL)
length++;
}
cout<<length<<endl;
for(int i=0;i<length;i++)
{
sum+=myArray[i]-'0';
}
cout<<sum;
}
Upvotes: -1
Reputation: 18652
There's a C++ Standard Library function called partial_sum()
that performs the series of sums you describe in a single pass over the input.
int sums[] = { 0, 3, 1, 4, 1, 5, 6 };
std::partial_sum(sums, sums + 7, sums);
// The results are left in sums[0]..sums[6]
Upvotes: 0
Reputation: 40145
#include <stdio.h>
int sum(const char ch){
static int sum = 0;
int retValue = sum;
sum += ch -'0';
return retValue;
}
int main(){
char nums[] = "314156";/* "31456" ? */
int size = sizeof(nums)/sizeof(char);
int i;
for(i=0 ; i< size ; ++i){
printf("%d time loop sum is %d\n", i+1, sum(nums[i]));
}
return 0;
}
DEMO
1 time loop sum is 0
2 time loop sum is 3
3 time loop sum is 4
4 time loop sum is 8
5 time loop sum is 9
6 time loop sum is 14
7 time loop sum is 20
Upvotes: 0
Reputation: 47503
A pseudo-code:
array of sums
sums[0] = 0; // That first time that you want to get 0!!
for i = 1 to length of str
sums[i] <- sums[i-1] + str[i]
Upvotes: 0
Reputation: 500267
Here is my advice: stop thinking in "loops" and start thinking in "steps". If the input string has n
characters, you have n+1
steps.
Now, ponder the following three questions:
k
, how can you compute the solution to step k+1
?Since this is homework, I'll let you take it from here.
Upvotes: 3
Reputation: 11687
If i am getting you right
double temp = 0;
for(int i = 0; i < str.Length; i++) // str is your complete number
{
temp += Convert.ToDouble(str[i]);
}
Hope it helps.
Upvotes: 0