can someone help me idk which mean to 1 do - c

I tried to do this:
#include <stdio.h >
int main(void) {
int common = 0;
int num1;
int num2;
int big;
int small;
int i;
printf("Enter the first number : ");
scanf("%d", &num1);
printf("Enter the second number : ");
scanf("%d", &num2);
if (num1 <= num2) {
num1 = small;
num2 = big;
}
else{
num1 = big;
num2 = small;
}
for (i = small ; ; i++)
{
}
I don't know what is meant by to 1 do. I'm beginner in algorithms
but I think it means i++ isn't?

The algorithm says to count from small down to 1. So the repetition should be i--, not i++. Use i >= 1 as the repetition condition to keep looping until you get to 1.
for (i = small; i >= 1; i--)

Related

C programming find min,max and sum without using recursion and arrays

I am trying to write a program that get bunch of numbers, n >= 5
The program asks from the user to enter n non-negative numbers and calculate min,max and the sum of the numbers.
In case the user enter negative numbers, the function asks to enter a positive number.
I have a problem with the first negative number, any clue what's wrong with the following code?
void main()
{
int x;
printf("Enter number:\n");
scanf("%d", &x);
if (x >= 5)
{
int max = 0, min, num1;
printf("Enter numbers: \n");
scanf("%d", &num1); //here was the error
min = num1;
int sum = num1;
for (int i = 1; i < x; i++)
{
scanf("%d", &num1);
while (num1 < 0)
{
{
printf("Enter again number: /n");
scanf("%d", &num1);
}
}
if (num1 > max)
max = num1;
else if (num1 < min)
min = num1;
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d", max, min, sum);
}
else
printf("invalid number!");
}
output:
Enter number:
8
Enter numbers:
-8
7
6
9
10
6
7
6
The max number is 10, and the min is -8, and the sum is 43
check if min<0 assign new entered value of num1 to it.
for (int i = 1; i < x; i++)
{
scanf("%d", &num1);
if (min < 0)
min = num1;
while (num1 < 0){
//rest of the code}
}
I have a problem with the first negative number, any clue what's wrong with the following code?
Do not read first number with special code, which lacks a negative test. Simplify and read just like other numbers.
Simply start min,max with extremes.
#include <limits.h>
...
int max = INT_MIN;
int min = INT_MAX;
int sum = 0;
printf("Enter numbers: \n");
for (int i = 0; i < x; i++) { // start at 0
int num1;
scanf("%d", &num1);
while (num1 < 0) {
printf("Enter again number: /n");
scanf("%d", &num1);
}
if (num1 > max)
max = num1;
// else if (num1 < min)
if (num1 < min)
min = num1;
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d",
max, min, sum);
you should put while after taking the input from the user to prevent any negative number also inside while there isn't need to put double cure prates
#include <stdio.h>
int main()
{
int x;
printf("Enter number:\n");
scanf("%d", &x);
if (x >= 5)
{
int max = 0, min, num1,sum;
printf("Enter numbers:\n");
sum = 0;
min = num1;
for (int i = 1; i<=x; i++)
{
scanf("%d",&num1);
while(num1<0)
{
printf("Enter again number: \n ");
scanf("%d", &num1);
}
if (num1 > max)
{ max = num1;}
if (num1<min)
{ min = num1;}
sum += num1;
}
printf("The max number is %d, and the min is %d, and the sum is %d", max, min, sum);
}
else
printf("invalid number!");
return 0;
}
````````````

Printing Fibonacci numbers up to n in C

The following code uses a user define function to print fibonacci numbers up to the user's choosing. The only problem is that the output includes the first n fibonacci numbers, followed by a zero. (e.g. 1,1,2,3,5,8,0) How can I get rid of the zero?
#include <stdio.h>
int fibonacci(int a);
int main()
{
int j, number;
printf("Enter the number of terms: ");
scanf("%d", &number);
printf("Fibonacci Series: ");
j = fibonacci(number);
printf("%d", j);
}
int fibonacci (int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
return 0;
}
I learned that we are supposed to return something when using functions other than main.
If you learned that, you should look for a better learning source. You could well have written:
void fibonacci(int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
}

How can I write a program that has an input of n and then calculates the sum of n numbers that the user enters?

I've written this so far:
#include<stdio.h>
int main()
{
int n = 0, i = 0, sum = 0, a = 0;
scanf("%d", &n);
while (i <= n);
{
scanf("\n%d", &a);
sum = sum + a;
i++;
}
printf("%d", sum);
}
but when I enter 8, for example, it won't allow me to add any other numbers.
What's the problem?
while (i <= n); --> while (i <= n). Drop the ;. With the;, the while() loop never ends and { scanf("\n%d", &a); ... is never entered.
Suggest using auto formatting - easy to catch problems like this.
Also, to read n values use < #BLUEPIXY
// while (i <= n)
while (i < n)
#Shabnam You can use this code
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}

Calculating the mean of an array

#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { NULL };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf_s("%d", &n);
while (n > 100 || n < 0)
{
printf("Amount of numbers should be less than 0 and more than 100\n");
scanf_s("%d", &n);
}
for (i = 0; i < n; i++)
{
scanf_s("%d", &arr[i + 1]);
}
printf("%f", mean(i-1, arr[i]));
system("pause");
}
When I run the code it gives me a read access error. The problem is with the mean() function I created but I don't know what's wrong. Help?
When I run the code it gives me a read access error. The problem is with the mean() function
Although the mean() function produces read access error, the actual problem is here:
printf("%f", mean(i-1, arr[i]));
You are not passing an array to your function, but its element (it is one past the end of what was written, too, so even the value that you pass is undefined).
You need to pass i for the length, because your mean() treats it as an exclusive upper limit, and you also need to pass arr for the array:
printf("%f", mean(i, arr));
Indexing problem when reading the data also needs to be fixed - you need to remove + 1:
scanf_s("%d", &arr[i]);
// ^
Try this:
#include<stdio.h>
#include<stdlib.h>
double mean(int i, int arr[])
{
int j, sum = 0;
for (j = 0; j < i; j++)
{
sum = arr[j] + sum;
}
return (float)sum/i;
}
int main()
{
int arr[100] = { 0 };
int i, n, sum = 0;
printf("How many numbers would you like to enter?");
scanf("%d", &n);
getchar();
while (n > 100 || n < 0)
{
printf("Amount of numbers should be more than 0 and less than 100\n");
scanf("%d", &n);
getchar();
}
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
getchar();
}
printf("%lf", mean(n, arr));
getchar();
}
Your call to the mean function was wrong. You have to pass the entire array, not just one element. Change arr[i] to arr.
Other minor modifications are what I did to make it run on my system. If it works for you otherwise, then great.

C Prog - While loop

Hi once again I have found myself stuck with coding. I have found the correct code but I do not understand why my initial coding does not work. Could anyone explain briefly the problem?
My initial code and the runnable code is posted below.
INITIAL CODE (does not work as expected)
#include <stdio.h>
#include <conio.h>
int main(void)
{
int counter;
int num1, num2, smallest;
counter = 1;
printf("Enter first number:");
scanf("%d", &num1);
smallest = num1;
while (counter <= 10) // User input phase
{
printf("Enter next numer:");
scanf("%d", &num2);
if (num1 < num2)
{
smallest = num1;
}
if (num2 < num1)
{
smallest = num2;
}
counter++;
}
printf("Smallest is %d", smallest);
getch();
}
RUNNABLE CODE
#include <stdio.h>
#include <conio.h>
int main(void)
{
int counter;
int num1, smallest;
counter = 1;
printf("Enter first number:");
scanf("%d", &num1);
smallest = num1;
while (counter <= 10) // User input phase
{
printf("Enter next numer:");
scanf("%d", &num1);
if (num1 < smallest)
{
smallest = num1;
}
if (smallest < num1)
{
smallest = smallest;
}
counter++;
}
printf("Smallest is %d", smallest);
getch();
}
In your first attempt you save the value of smallest each loop, but you get rid of it because of the next loop will check always num1 and new entered number in num2.
In the second code, the working one, the loop is keeping track of the last smallest found.
A little thing, the code chunk
if (num1 < smallest)
{
smallest = num1;
}
if (smallest < num1)
{
smallest = smallest;
}
the second if does not make sense, you can write
if (num1 < smallest)
{
smallest = num1;
}
else
{
smallest = smallest;
}
or better
if (num1 < smallest)
{
smallest = num1;
}

Resources