Sum and get the average of the values in the array, and stop to ask a number when the sum exceeds 10,000.I need to clear it with an example of these conditions.
The language fit that need the example is in C
my code
```
#include <stdio.h>
int main()
{
int array[10];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (negative, positve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
}
```
Answer of your Question
#include <stdio.h>
int main()
{
int array[10];
int num, negative_sum = 0, positive_sum = 0,j;
static int i=0;
float total = 0.0, average;
label:
j=i;
printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (negative, positve and zero) \n", num);
for (i; i < (j+num); i++)
{
scanf("%d", &array[i]);
}
i=j;
printf("Input array elements \n");
for (i; i < (j+num); i++)
{
printf("%d\n", array[i]);
}
i=j;
for (i; i < (j+num); i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + (float)array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers = %d\n", negative_sum);
printf("Sum of all positive numbers = %d\n", positive_sum);
printf("\n Average of all input numbers = %.2f\n", average);
if(total<10000)
{
printf("Total is less than 10000 and Now total is : %.2f\n",total);
goto label;
}}
it is the answer of your question.program will run when your total is exceed upto 10000.
Related
*i want to copy inputed array in sum_of_elements function as argument and then sum all the elements of array, but i am getting output 0.
#include <stdio.h>
int i, num, sum;
int sum_of_elements(int arr[]) {
for (i = 0; i < num; i++) {
for (i = 0; sum = 0, i < num; i++) {
sum += arr[i];
}
return sum;
}
}
int main() {
printf("enter number of digits you want to add\n");
scanf("%d", & num);
int arr[num];
for (i = 0; i < num; i++) {
printf("enter number %d\n", i + 1);
scanf("%d", & arr[i]);
}
int total = sum_of_elements(arr);
printf("%d", total);
return 0;
Look at this line of code:
for (i = 0; sum = 0, i < num; i++) {
This resets sum to 0 every loop.
It should be
for (i = 0, sum = 0; i < num; i++) {
But it's probably better to do this:
sum = 0;
for (i = 0; i < num; i++) {
The issue was with the double for loop in your sum_of_elements function.
Removing the extra for loop, resolves the error.
#include <stdio.h>
int i, num, sum;
int sum_of_elements(int arr[]) {
for (i = 0; i < num; i++) {
sum += arr[i];
}
return sum;
}
int main() {
printf("enter number of digits you want to add\n");
scanf("%d", & num);
int arr[num];
for (i = 0; i < num; i++) {
printf("enter number %d\n", i + 1);
scanf("%d", &arr[i]);
}
int total = sum_of_elements(arr);
printf("%d", total);
return 0;
}
I'm writing a program where the user enters numbers and the program will find MAX and MIN and the position of these numbers. I want to give the user a choice for the program to fill in the numbers for him using rand().
It's working almost perfectly: the program will find the MAX number with the position but the problem occurs when printing MIN number with position -- it always prints number 8 and position 1.
Where is the problem?
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct elementposition {
int min;
int max;
int positionMax;
int positionMin;
} elementposition;
int main() {
struct elementposition minmax;
srand(time(NULL));
int a[500], i;
int c = sizeof(a) / sizeof(a[0]);
char y;
printf("How many numbers you want to enter: ");
scanf("%d", &c);
minmax.positionMax = minmax.positionMin = 0;
printf("Want to fill with random numbers? (Y/N)");
scanf(" %c", &y);
if (y == 'Y' || y == 'y') {
for (i = 0; i < c; i++) {
a[i] = rand() % 10000 + 1;
if (minmax.max < a[i]) {
minmax.max = a[i];
minmax.positionMax = i;
}
if (minmax.min > a[i]) {
minmax.min = a[i];
minmax.positionMin = i;
}
}
for (i = 0; i < c; i++) {
printf("Number #%d: %d\n", i + 1, a[i]);
}
} else {
printf("------------------------------------ \n");
printf("Enter (%d) numbers: \n", c);
scanf("%d", &a[0]);
minmax.max = minmax.min = a[0];
for (i = 1; i < c; i++) {
scanf("%d", &a[i]);
if (minmax.max < a[i]) {
minmax.max = a[i];
minmax.positionMax = i;
}
if (minmax.min > a[i]) {
minmax.min = a[i];
minmax.positionMin = i;
}
}
}
printf("\nMax number is %d, number position %d. \n", minmax.max, minmax.positionMax + 1);
printf("Min number is %d, number position %d. \n", minmax.min, minmax.positionMin + 1);
printf("------------------------------------ \n");
getch();
return 0;
}
You never initialize minmax.min nor minmax.max in the random case. The code has undefined behavior because it depends on uninitialized values which may be anything, including trap values on some rare architectures.
You should separate the input/generation phase from the scanning phase and use a common loop for that. Also check that c is positive and does not exceed the length of the array.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
typedef struct elementposition {
int min;
int max;
int positionMax;
int positionMin;
} elementposition;
int main() {
struct elementposition minmax;
int a[500];
int i, count, len = sizeof(a) / sizeof(a[0]);
char y = 'y';
printf("How many numbers you want to enter: ");
if (scanf("%d", &count) != 1 || count < 1 || count > len) {
printf("invalid count\n");
return 1;
}
printf("Want to fill with random numbers? (Y/N)");
scanf(" %c", &y);
if (y == 'Y' || y == 'y') {
srand(time(NULL));
for (i = 0; i < count; i++) {
a[i] = rand() % 10000 + 1;
printf("Number #%d: %d\n", i + 1, a[i]);
}
} else {
printf("Enter (%d) numbers:\n", c);
for (i = 0; i < count; i++) {
if (scanf("%d", &a[i]) != 1) {
printf("invalid input\n");
return 1;
}
}
}
minmax.positionMax = minmax.positionMin = 0;
minmax.max = minmax.min = a[0];
for (i = 1; i < count; i++) {
if (minmax.max < a[i]) {
minmax.max = a[i];
minmax.positionMax = i;
}
if (minmax.min > a[i]) {
minmax.min = a[i];
minmax.positionMin = i;
}
}
printf("------------------------------------\n");
printf("Max number is %d, number position %d.\n", minmax.max, minmax.positionMax + 1);
printf("Min number is %d, number position %d.\n", minmax.min, minmax.positionMin + 1);
printf("------------------------------------\n");
getch();
return 0;
}
You use minmax.min and minmax.max before initializing them. Here the problem for finding the min is probably that the minmax.min happens to initialy contain the value 8 and that all the values are greater.
The common way is to initialize the min to the highest possible value and max to the lowest one. As you use int values:
struct elementposition minmax = { INT_MAX, INT_MIN };
should be enough.
I am trying to show the sum of the prime factors of a given number and
I'm having difficulties displaying the prime factors in my output.
Sample Output:
Input number: 6
Factors are: 1 2 3
Sum of its factor: 1 +2 +3 =6
I am able to show the sum but I want to show the 1+2+3=6 like in the sample above where the factors are 1 2 3.
Can you help me correct my syntax to achieve this? Thanks in advance.
Here's my code:
#include <stdio.h>
int main() {
int i, j, num, isPrime, sum;
printf("Input number: ");
scanf("%d", &num);
printf("Factors are: ", num);
for (i = 1; i <= num; i++) {
if (num % i == 0) {
isPrime = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime == 1) {
printf("%d ", i);
sum += i;
}
}
}
printf("\nSum of its factor : %d", sum);
return 0;
}
Your code actually has undefined behavior because sum is not initialized to 0. It produces the correct sum only by chance.
You can store the factors in an array, or even construct the expression as you go with sprintf. The maximum length of the expression is not very large as there can be at most 9 different prime factors (29!! > 232)
Here is a modified version:
#include <stdio.h>
int main() {
char expr[9 * 11 + 1];
int i, j, pos, num, isPrime, sum;
printf("Input number: ");
if (scanf("%d", &num) != 1)
return 1;
printf("Factors are: 1"); // always include 1
pos = 0;
expr[pos] = '\0';
sum = 1;
for (i = 2; i <= num; i++) {
if (num % i == 0) {
isPrime = 1;
for (j = 2; j * j <= i; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime == 1) {
pos += sprintf(expr + pos, "+%d", i);
printf(" %d", i);
sum += i;
}
}
}
printf("\nSum of its factors: 1%s = %d\n", expr, sum);
return 0;
}
Output:
Input number: 6
Factors are: 1 2 3
Sum of its factors: 1+2+3 = 6
Here is a more robust and much faster version that does not have undefined behavior for very large values of num:
#include <stdio.h>
int main() {
char expr[9 * 11 + 1];
int i, pos, num;
unsigned sum;
printf("Input number: ");
if (scanf("%i", &num) != 1)
return 1;
printf("Factors are: 1"); // always include 1
pos = 0;
expr[pos] = '\0';
sum = 1;
for (i = 2; num / i >= i; i++) {
if (num % i == 0) {
pos += sprintf(expr + pos, "+%d", i);
printf(" %d", i);
sum += i;
do { num /= i; } while (num % i == 0);
}
}
if (num != 1) {
pos += sprintf(expr + pos, "+%d", num);
printf(" %d", num);
sum += num;
}
printf("\nSum of its factors: 1%s = %u\n", expr, sum);
return 0;
}
Test:
Input number: 0x7fffffff
Factors are: 1 2147483647
Sum of its factors: 1+2147483647 = 2147483648
Since you want to print all the prime factors twice, you should do that in a way so that you can avoid duplicated code. Here is an idea:
#include <stdio.h>
/* Return the smallest prime that is smaller than or equal to n */
/* Assumes that the argument is greater than 1 */
int getFirst(int n)
{
int i;
for(i = 2; i <= n; i++)
if(n % i == 0)
return i;
}
int main()
{
int num, x, tmp, sum=0;
scanf("%d", &num);
tmp = num;
printf("Factors are: ");
while(1) {
x = getFirst(tmp);
printf("%d ", x);
if (x == tmp) /* If we are at the last prime */
break;
tmp /= x;
}
printf("\n");
printf("Sum of factors is: ");
tmp = num;
while(1) {
x = getFirst(tmp);
printf("%d ", x);
sum += x;
if(x == tmp) /* If we are at the last prime */
break;
printf("+ ");
tmp /= x;
}
printf("= %d\n", sum);
}
But as has been pointed out in the comments. 1 is not a prime, and that's why I excluded it.
I want to generate the arithmetic average from an array, but only with values from a certain range (here from -5 to 5)
Is this code ok?
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, average;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 10 || n <= 0)
{
printf("Error! number should in range of (1 to 10).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
if( num[i]< 5 && num[i]>-5){
sum+= num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
edit : I am sorry I must have missed copying the whole code in th eheat of the moment.
It is a simple question I know bu I cannot seem to get it to work. Maybe the lack of sleep is making me go insane
Your code is wrong.
And it's poorly formatted and that's the reason why you don't see why it's wrong:
Your poorly formatted code:
...
for(i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i+1);
scanf("%f", &num[i]);
if( num[i]< 5 && num[i]>-5){
sum+= num[i];
} // it looks as if the for loop end here, but it doesn't
average = sum / n;
printf("Average = %.2f", average);
return 0;
} // the for loop actually ends here
The same code formatted correctly:
for (i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
if (num[i]< 5 && num[i]>-5) {
sum += num[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
} // <<< the for loop ends here
Correct code (look at the comments for an explanation):
...
int nbofnumbers = 0; // number of numbers in the interval [-5,5]
for (i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
if (num[i]< 5 && num[i]>-5) {
nbofnumbers++;
sum += num[i];
}
} // for loop must end here
average = sum / nbofnumbers; // we divide by the number of numbers
// in the interval [-5,5], not by n
printf("Average = %.2f", average);
return 0;
...
#include <stdio.h>
#include <string.h>
void main()
{
int smallest, secondsmallest;
int array[100], size, i;
printf("\n How many elements do you want to enter: ");
scanf("%d", &size);
printf("\nEnter %d elements: ", size);
for (i = 0 ; i < size; i++)
scanf("%d", &array[i]);
if (array[0] < array[1]) {
smallest = array[0];
secondsmallest = array[1];
}
else {
smallest = array[1];
secondsmallest = array[0];
}
for (i = 2; i < size; i++) {
if (array[i] < smallest) {
secondsmallest = smallest;
smallest = array[i];
}
else if (array[i] < secondsmallest) {
secondsmallest = array[i];
}
}
printf(" \nSecond smallest element is %d", secondsmallest);
printf(" \n smallest element is %d", smallest);
}
input:0 0 1 2
output:smallest is 0, second smallest is 0
i want to get 0,1 as output.i do not want to use sorting here.
how can i improve my code.
You are not handling the case of duplicate number. So I have modified your code. Please try this and let me know.
#include <stdio.h>
#include <string.h>
void main()
{
int smallest, secondsmallest;
int array[100], size, i;
printf("\n How many elements do you want to enter: ");
scanf("%d", &size);
printf("\nEnter %d elements: ", size);
for (i = 0 ; i < size; i++)
scanf("%d", &array[i]);
if (array[0] < array[1]) {
smallest = array[0];
secondsmallest = array[1];
}
else {
smallest = array[1];
secondsmallest = array[0];
}
for (i = 2; i < size; i++) {
if (array[i] < smallest) {
secondsmallest = smallest;
smallest = array[i];
}
else if (smallest == secondsmallest){
smallest = secondsmallest;
secondsmallest = array[i];
}
else if (array[i] < secondsmallest && array[i] > smallest) {
secondsmallest = array[i];
}
}
printf(" \nSecond smallest element is %d\n", secondsmallest);
printf(" \n smallest element is %d\n", smallest);
}