# Answer to Question #69211 in C for sulthana kormath

Question #69211

Ram is a school boy. He has n good friends numbered from 1 to n. Let this be denoted by an Array A. The friend with higher array index is closer to Ram. It's his birthday today and hence he wants to distribute his birthday chocolates in a way such that A[i+1]=A[i]+3. Now he had n boxes numbered from 1 to n for each of his friend and his mother had filled them with some random number of chocolates, However Ram has made up his mind to distribute the the chocolates in the way described above. He wants to know if it is possible to re-arrange the chocolates in the boxes to have a distribution pattern he likes.

Expert's answer

#include <stdlib.h> /* rand() */

#include <time.h> /* time() */

#include <stdio.h> /* printf() */

#define N_OF_FRIENDS 3

#define MAX_MOTHER_CARE 10

#define MIN_MOTHER_CARE 1

int main() {

const unsigned SIZE = N_OF_FRIENDS;

int ArrayA[N_OF_FRIENDS] = { 0 }; /* initialize all the elements */

unsigned index = 0;

int Total = 0;

int Sum3 = 0;

srand((unsigned) time(0)); /* set new seed for a generator each time */

for (index = 0; index < SIZE; index++) {

ArrayA[index] /* generating numbers in a range */

= rand() % (MAX_MOTHER_CARE - MIN_MOTHER_CARE + 1) + MIN_MOTHER_CARE;

printf("%d ", ArrayA[index]);

}

printf("\n");

/* We just need to check that total number of chocolates (Total) equals

* A[1] + (A[1] + 3) + (A[1] + 3 + 3) + ... + (A[1] + 3 * (n - 1)), equals

* n * A[1] + 0 + 3 + 6 + ... + 3 * (n - 1), where A[1] is a natural number.

* Or, in other words, the difference between total number of chocolates

* and (0 + 3 + 6 + ... + 3 * (n - 1)) is divisible by n and positive.

* Or, in other words, Total - (0 + 3 + 6 + ... + 3 * (n - 1)) % n = 0 and

* that difference is positive.

*/

for (index = 0; index < SIZE; index++) {

Total += ArrayA[index]; /* add and assign */

Sum3 += index * 3; /* add and assign */

}

if ((Total - Sum3) % 3 == 0 && (Total - Sum3) > 0) {

printf("Yes, it is possible, %d, %d, A1=%d\n",

Total, Sum3, (Total - Sum3) / 3);

} else {

printf("No, it is impossible, %d, %d\n",

Total, Sum3);

}

return 0;

}

#include <time.h> /* time() */

#include <stdio.h> /* printf() */

#define N_OF_FRIENDS 3

#define MAX_MOTHER_CARE 10

#define MIN_MOTHER_CARE 1

int main() {

const unsigned SIZE = N_OF_FRIENDS;

int ArrayA[N_OF_FRIENDS] = { 0 }; /* initialize all the elements */

unsigned index = 0;

int Total = 0;

int Sum3 = 0;

srand((unsigned) time(0)); /* set new seed for a generator each time */

for (index = 0; index < SIZE; index++) {

ArrayA[index] /* generating numbers in a range */

= rand() % (MAX_MOTHER_CARE - MIN_MOTHER_CARE + 1) + MIN_MOTHER_CARE;

printf("%d ", ArrayA[index]);

}

printf("\n");

/* We just need to check that total number of chocolates (Total) equals

* A[1] + (A[1] + 3) + (A[1] + 3 + 3) + ... + (A[1] + 3 * (n - 1)), equals

* n * A[1] + 0 + 3 + 6 + ... + 3 * (n - 1), where A[1] is a natural number.

* Or, in other words, the difference between total number of chocolates

* and (0 + 3 + 6 + ... + 3 * (n - 1)) is divisible by n and positive.

* Or, in other words, Total - (0 + 3 + 6 + ... + 3 * (n - 1)) % n = 0 and

* that difference is positive.

*/

for (index = 0; index < SIZE; index++) {

Total += ArrayA[index]; /* add and assign */

Sum3 += index * 3; /* add and assign */

}

if ((Total - Sum3) % 3 == 0 && (Total - Sum3) > 0) {

printf("Yes, it is possible, %d, %d, A1=%d\n",

Total, Sum3, (Total - Sum3) / 3);

} else {

printf("No, it is impossible, %d, %d\n",

Total, Sum3);

}

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment