Simple c array calculator drops the first number - c

This is part of my assignment. I have to make a calculator using arrays.
this is the addition part of it.
It works fine but the problem is, it drops the first digit. array size is for example 10 . it doesn't show the first digit whenever addition has remainder.
for example i want to add 5 9 7 5 3 1 0 0 0 0 with 5 0 8 6 4 2 0 0 0 0. the true answer is 11061730000 but this program shows 1061730000 (drops first 1).
how do i fix this behavior??
code:
#include <stdio.h>
#define SIZE_MAX 10
#define SIZE_USE SIZE_MAX-1
int main()
{
int i;
int inum_firstPTR[SIZE_MAX] = {5, 9, 7, 5, 3, 1, 0, 0, 0, 0};
int inum_secondPTR[SIZE_MAX] = {5, 0, 8, 6, 4, 2, 0, 0, 0, 0};
int add_resultPTR[SIZE_MAX] = {0}; //initializing result array
for (i = SIZE_USE; i >= 0; i--)
{
if (add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] < 10)
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
else
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] - 10;
inum_firstPTR[i - 1] = inum_firstPTR[i - 1] + 1;
}
}
puts("");
for (i = 0; i < SIZE_MAX; i++)
{
printf("%d", add_resultPTR[i]);
}
puts("");
return 0;
}

for (i = SIZE_USE; i >= 0; i--)
{
if (add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] < 10)
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
else //carry
{
if ( i > 0) // normal carry to next element
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i] - 10;
inum_firstPTR[i - 1] = inum_firstPTR[i - 1] + 1;
}
else // element [0], no more elements to carry to
{
add_resultPTR[i] = inum_firstPTR[i] + inum_secondPTR[i];
}
}
}

addition of 5+5+carry1 is 11 but add_resultPTR[0] can only store 1 and there is no position before [0] to store carry1 therefore it only prints from 1.
moreover, 11061730000 are 11 digits how do you suppose to fit into an array of SIZE_MAX 10.
define SIZE_MAX as 11 and then to prevent from overflow store addition of inum_firstPTR[SIZE_MAX] and inum_firstPTR[SIZE_MAX] from add_resultPTR[0] to add_resultPTR[10] and print it in reverse i.e. if add_resultPTR[10] is 1 then from [10] to [0] otherwise from [9] to [0].

Related

print evens number and odds number from an array

Declare an array containing these number and print the evens numbers and odd numbers
Now I initialized an array that containing 11 integers.
Here is my code
#include <stdio.h>
int main(void) {
int nums[11] = {11,3,9,7,6,10,13,17,2,8,3}; // create an variables that store integers
int evens[11] = {0}; // initialized an array to store even numbers
int odds[11] = {0}; // initialized an array to store even numbers
int length = sizeof(nums) / sizeof(nums[0]); // get the length of nums
int nums_index = 0;
int evens_index = 0;
int odds_index = 0;
for (nums_index; nums_index < length;nums_index++) {
if (nums[nums_index] % 2 == 0) {
evens[evens_index] = nums[nums_index];
evens_index++;
}
else if(nums[nums_index] % 2 != 0) {
odds[odds_index] = nums[nums_index];
odds_index++;
}
printf("%d\n",evens[evens_index]);
printf("%d\n",odds[odds_index]);
}
return 0;
}
The major question is whether the output has problems when I compile my code.
The output is :0 11 0 3 0 9 0 7 6 0 10 0 0 13 0 17 2 0 8 0 0 3
Why it could happened?
Thank you all.
You need separate indexing for each array, advancing the index for evens and odds only when nums[i] value is one of the two.
Otherwise you would get sort of a copy of nums with zeroes in place of those numbers of the opposite type (odd/even).
For instance:
int j = 0;
int k = 0;
for (int i = 0; i < length; i++) {
if (nums[i] % 2 == 0) {
evens[j] = nums[i];
j++;
}
else if(nums[i] % 2 != 0) {
odds[k] = nums[i];
k++;
}
printf("%d\n",evens[i]);
printf("%d\n",odds[i]);
}
This will compose the arrays like:
11 3 9 7 13 17 3 0 0 0 0 --- for odds
6 10 2 8 0 0 0 0 0 0 0 0 --- for evens
The second problem is that you are printing inside the loop, firstly a value from evens and immediately after a value for odds.
So if you want to display them nice and separate, you can move both printf outside the first loop, then looping again on each result array for displaying it completely, before proceding to the other.
#include <stdio.h>
void PrintNumbers(int*, int);
int main(void) {
int nums[11] = {11,3,9,7,6,10,13,17,2,8,3}; // create an variables that store integers
int evens[11] = {0}; // initialized an array to store even numbers
int odds[11] = {0}; // initialized an array to store even numbers
int length = sizeof(nums) / sizeof(nums[0]); // get the length of nums
int nums_index = 0;
int evens_index = 0;
int odds_index = 0;
for (nums_index; nums_index < length; nums_index++)
{
if (nums[nums_index] % 2 == 0)
{
evens[evens_index] = nums[nums_index];
evens_index++;
}
else if(nums[nums_index] % 2 != 0)
{
odds[odds_index] = nums[nums_index];
odds_index++;
}
}
printf("Original List: ");
PrintNumbers(nums, length);
printf("Even numbers: ");
PrintNumbers(evens, length);
printf("Odd numbers: ");
PrintNumbers(odds, length);
return 0;
}
void PrintNumbers(int* numbers, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d, ", numbers[i]);
}
printf("\n");
}
Output:
Original List: 11, 3, 9, 7, 6, 10, 13, 17, 2, 8, 3,
Even numbers: 6, 10, 2, 8, 0, 0, 0, 0, 0, 0, 0,
Odd numbers: 11, 3, 9, 7, 13, 17, 3, 0, 0, 0, 0,

Loop through odd numbers and 2

Is it possible in C to have a fast for/while loop that loops through the odd numbers and 2? Without using arrays.
So I'd like it to loop through {1, 2, 3, 5, 7, 9, ..}
Of course. Here is a pretty straight forward way.
for(int i=1; i<N; i++) {
if(i>3) i++;
// Code
}
A bit more hackish variant:
for(int i=1; i<N; i+=1+(i>2)) {
// Code
}
But I think in this case that the most readable variant would be something like:
// Code for 1 and 2
// Then code for 3,5,7 ...
for(int i=3; i<N; i+=2) {
// Code
}
Another option
for(int i=1;;++i) // you didn't specify a limit
{
switch(i)
{
default:
if(!(i&1))continue;
case 1:
case 2:
DoSomething(i):
}
}
Another alternative which does use an array but only a small one that is a constant size of two elements no matter how many numbers in the sequence would be:
{
int i;
int iray[] = {1, 2};
int n = 15;
for (i = 1; i < n; i += iray[i > 2]) {
printf (" i = %d \n", i);
// code
}
}
which produces:
i = 1
i = 2
i = 3
i = 5
i = 7
i = 9
i = 11
i = 13
Extending this alternative to other sequences
And this alternative can be extended to other sequences where there is a change of a similar nature. For instance if the desired sequence was
1, 2, 3, 5, 8, 11, ..
Which involves several changes in the sequence. Beginning at 1 an increment of 1 is used followed by a first increment change beginning at 3 where an increment of 2 is used followed by a second change in the sequence beginning at 5 where an increment of 3 is used, you can make the following modification.
{
int i;
int iray[] = {1, 2, 3}; // increment changes
int n = 15;
// calculate the increment based on the current value of i
for (i = 1; i < n; i += iray[(i > 2) + (i > 3)]) {
printf (" i = %d \n", i);
// code
}
return 0;
}
which would produce:
i = 1
i = 2
i = 3
i = 5
i = 8
i = 11
i = 14
#include <stdio.h>
int main()
{
for(unsigned x = 0; x < 10; x++)
printf("%u%s element - %u\n",x + 1, !x ? "st" : x == 1 ? "nd" : x == 2 ? "rd" : "th", !x + x * 2 - (x >= 2));
return 0;
}
no jumps calculating in the !x + x * 2 - (x >= 2) so no pipeline flushes.

Selection of maximum sub-array from the array

Given an array of length n, it is required to find the maximum sum of elements one can choose if it is not allowed to choose more than two consecutive elements of the array. For example;
n=5;
arr[5] = {10,3,5,7,3};
Output : 23
10+3+7+3=23
So I have written this code;
#include <stdio.h>
#include <stdlib.h>
int max=0;
void solve(int arr[],int ind,int sum,int n,int count)
{
if(ind==n){
if(sum>max)
max=sum;
}
else{
sum+=arr[ind];
if(ind==n-1)
solve(arr,ind+1,sum,n,1);
if(ind==n-2 && count>1)
solve(arr,ind+2,sum,n,1);
if(ind<n-1 && count<2){
count++;
solve(arr,ind+1,sum,n,count);
}
if(ind<n-2)
solve(arr,ind+2,sum,n,1);
if(ind<n-3)
solve(arr,ind+3,sum,n,1);
}
}
int main()
{
int n;
scanf("%d",&n);
int i=0,arr[n];
while(i<n){
scanf("%d",&arr[i]);
i++;
}
int count=1;
//going into all three possibilities
solve(arr,0,0,n,count);
solve(arr,1,0,n,count);
solve(arr,2,0,n,count);
printf("%d\n",max);
return 0;
}
This program produces the expected outputs for n<1000 but shows runtime error (SIGSEGV) for larger inputs. What may be the reason?
More effective solutions are also welcome.....
use dynamic programming
DP[i]: maximum from "i" index
there are 7 cases:
1- use the first and second elements
2- use the second and third elements
3- use the first and third elements
4- use only the first element
5- use only the second element
6- use only the third element
7- use none of the elements
int F(int[] a)
{
if (a.Length == 1)
{
return Max(a[0], 0);
}
int n = a.Length;
int[] DP = new int[n];
DP[n - 1] = Max(a[n - 1], 0);
DP[n - 2] = DP[n - 1] + Max(a[n - 2], 0);
for (int i = n - 3; i >= 0; i--)
{
DP[i] = Max(a[i], 0) + Max(a[i + 1], 0) + (i + 3 < n ? DP[i + 3] : 0);// first and second
DP[i] = Max(DP[i], Max(a[i + 1], 0) + Max(a[i + 2], 0) + (i + 4 < n ? DP[i + 4] : 0));// second and third
DP[i] = Max(DP[i], Max(a[i + 0], 0) + Max(a[i + 2], 0) + (i + 4 < n ? DP[i + 4] : 0));// first and third
DP[i] = Max(DP[i], Max(a[i + 0], 0) + (i + 2 < n ? DP[i + 2] : 0));// first
DP[i] = Max(DP[i], Max(a[i + 1], 0) + (i + 3 < n ? DP[i + 3] : 0));// second
DP[i] = Max(DP[i], Max(a[i + 2], 0) + (i + 4 < n ? DP[i + 4] : 0));// third
DP[i] = Max(DP[i], DP[i + 1]);// none
}
return DP[0];
}
example1:
int[] a = new int[] { 10, 3, 5, 7, 3 };
writer.WriteLine(F(a));
output:
23
example2:
int[] a = new int[] { 1, 5, 2, 6, 9, 8, 20, 12, 41, 3, 0, 9, 95, 6, 74, 85, 20, 14, 26, 35, 14, 72, 15 };
writer.WriteLine(F(a));
output:
496
Implementation in C
This problem has a fairly simple dynamic programming solution.
Each item in the array represents a binary choice: it can either be selected or not. But if two consecutive items are selected, then the next item cannot be selected. So for each item in the array we need to keep track of three sums
the best sum if the current item is not selected
the best sum if the current item is selected, and the previous item was not selected
the best sum if the current item is selected, and the previous item was selected
Here's the code:
#include <stdio.h>
#define max3(a) (a[0]>a[1] ? a[0]>a[2]?a[0]:a[2] : a[1]>a[2]?a[1]:a[2])
int main( void )
{
int array[] = { 10,3,7,55,60,62,4,2,5,42,8,9,12,5,1 };
int N = sizeof(array) / sizeof(array[0]);
int dp[N][3];
dp[0][0] = 0;
dp[0][1] = array[0];
dp[0][2] = 0;
for ( int i = 1; i < N; i++ )
{
dp[i][0] = max3(dp[i-1]);
dp[i][1] = dp[i-1][0] + array[i];
dp[i][2] = dp[i-1][1] + array[i];
}
printf( "%d\n", max3(dp[N-1]) );
}
The output of this program is 208. To understand how that was computed, take a look at the contents of the dp array:
Note that the correct path through the dp array is not known until the end. In this example, two endpoints have the same sum, so there are two paths through the array that give the same answer. The two paths represent these choices:
array: 10 3 7 55 60 62 4 2 5 42 8 9 12 5 1
red: 10 +7 +60+62 +2 +42+8 +12+5 = 208
blue: 10 +7 +60+62 +5+42 +9+12 +1 = 208

C - Recursive, cumulative sum of an array

I've been tasked with making a recursive function that takes an array of numbers, and turns it into an array of the cumulative sum of all the numbers up to this point, thus:
1, 2, 3, 4, 5 becomes 1, 3, 6, 10, 15
This is what I came up with:
#include <stdio.h>
int cumul(int tab[], int length, int ind) {
if (ind > 0) {
tab[ind] += tab[ind-1];
}
if (ind < length) {
cumul(tab, length, ind+1);
}
return 0;
}
int main() {
int ind;
int tab[6] = {1, 2, 3, 4, 5, 6};
int length = sizeof(tab)/sizeof(tab[0]);
for (ind = 0; ind < length; ind++) {
printf("%d ", tab[ind]);
}
printf("\n");
cumul(tab, length, 0);
for (ind = 0; ind < length; ind++) {
printf("%d ", tab[ind]);
}
printf("\n");
return 0;
}
It works well in most cases but I've hit a snag for oddly specific arrays:
For example, it doesn't work for tab[6] = {1, 2, 3, 4, 5, 6}, here's the output:
1 2 3 4 5 6
1 3 6 10 15 21 27 7 4196016 0 -1076574208 32528 -1609083416 32767 -1609083416 32767 0 1 4195802 0 0 0 -1815242402 30550560 4195424 0 -1609083424
I have no idea why it goes bonkers. It works fine for just about any tab[5] and tab[7] arrays I tried, but fails for every tab[6] array I tried.
The problem occurs when ind reaches length-1. For example, if length is 6, and ind is 5, then the recursive call is
cumul(tab, 6, 6); // length=6 ind+1=6
At the next level of recursion, after the if ( ind > 0 ), the code will do this
tab[6] += tab[5]; // ind=6 ind-1=5
That results in undefined behavior because you're writing beyond the end of the array.
You could check the upper bound in the first if statement, e.g.
if ( ind > 0 && ind < length )
But it's better to just avoid the recursive call by changing the second if statement to
if ( ind < length - 1 )
Either change avoids the situation where you access tab[length].

How do I compute the number of valleys in a sequence of numbers?

Given a sequence of digits, a valley is defined as the region in the sequence that is surrounded (to the left and right) by higher values. The task is to find the number of valleys in the sequence.
For example,
{9,8,7,7,8,9} has one valley at {7,7}
{9,8,7,7,8,6,9} has two valleys at {7,7} and {6}
{7,8,9,8,7} has no valleys
The code I have to compute the number of valleys is as follows:
#include <stdio.h>
#define SIZE 40
int main()
{
int input;
int store[SIZE];
int i = 0;
int j;
int valley = 0;
int count = 0;
printf("Enter sequence: ");
scanf("%d", &input);
while(input != -1)
{
store[i] = input;
i++;
scanf("%d", &input);
}
count = count + i;
for(i = 1; i < count; i++)
{
for(j = i; j < i + 1; j++)
{
if((store[j-1] > store[j]) && (store[j] < store[j+1]))
{
valley = valley + 1;
break;
}
}
}
printf("Number of valleys: %d", valley);
return 0;
}
I am able to display the correct answer if the input is "3 2 1 2 3". However, if in between the number is equal to another and they are side by side (for example, "3 1 1 2"), the program will compute the wrong answer.
How do I go about writing the program so that I am able to display the correct number of valleys?
Look for slope changes from down to up.
Rather than a double nested for loop, march along looking for slope changes from down to up. Consider any slope of 0 to be the same as the previous slope.
size_t Valley(const int *store, size_t count) {
size_t valley = 0;
int slope = -1;
size_t i;
// Find first down slope
for (i = 1; i < count; i++) {
if (store[i] < store[i - 1]) {
break;
}
}
for (; i < count; i++) {
int newslope = (store[i] > store[i - 1]) - (store[i] < store[i - 1]);
// Loop for slope changes
if (newslope == -slope) {
if (newslope > 0)
valley++;
slope = newslope;
}
}
return valley;
}
Test code.
void Vtest(const int *store, size_t count) {
size_t n = Valley(store, count);
printf("%zu %zu\n", count, n);
}
void Vtests(void) {
int a1[] = { 9, 8, 7, 7, 8, 9 };
Vtest(a1, sizeof a1 / sizeof a1[0]);
int a2[] = { 9, 8, 7, 7, 8, 6, 9 };
Vtest(a2, sizeof a2 / sizeof a2[0]);
int a3[] = { 7, 8, 9, 8, 7 };
Vtest(a3, sizeof a3 / sizeof a3[0]);
int a4[] = { 3, 2, 1, 2, 3 };
Vtest(a4, sizeof a4 / sizeof a4[0]);
int a5[] = { 8, 7, 7, 8, 6 };
Vtest(a5, sizeof a5 / sizeof a5[0]);
}
int main(void) {
Vtests();
return 0;
}
Output
6 1
7 2
5 0
5 1
5 1
The problem is here:
if((store[j-1] > store[j] )&&(store[j] < store[j+1]))
In both comparations you are using index j, so this program finds only valleys with length 1. Try this modification:
if((store[i-1] > store[i] )&&(store[j] < store[j+1]))
Also I am not sure, that it is right to break; in this situation. But it is not clear now, which answer is correct in case 3 1 2 3 - one (1) or two (1 and 1 2). From your first example we can see, that right answer is one, but it is not obvious from the definition.
Depending on whether you define valley as a higher value to the IMMEDIATE left/right of a given point you may need to adjust the Valley function provided by chux as follows:
size_t Valley (const int *store, size_t count) {
...
i++;
for (; i < count; i++) {
int newslope = (store[i] > store[i - 1]) - (store[i] < store[i - 1]);
if (newslope == -slope) {
if (newslope > 0)
valley++;
}
slope = newslope;
}
...
}
output:
$ ./bin/valleyt
6 0
7 1
5 0
5 1
5 0
This is a supplement to the answer provided by chux, and the input data is as he provided in his answer. This code just limits the definition of a valley to being created by 3 adjacent points. (a special case of the general answer of a change from negative to positive slope with intervening equivalent points)

Resources