How can I print the value of variable y? - c

I just switched to C from Python and I cannot understand why I cannot print y like a normal integer.
For eg if the value of sum is greater than maxsum when i=4 I want to print y as 4 too. I tried using i directly but I was not able to print a normal value.
#include <stdio.h>
#include <stdlib.h>
int main() {
int maxsum = 0;
int list[15];
int initialindex[15];
for (int j = 0; j < 15; j++) {
list[j] = rand() % 300 - 150;
}
int sum = 0;
int count = 0;
int y = 0;
for (int i = 0; i < 15; i++) {
count += 1;
if (sum + list[i] <= 0) {
initialindex[i] = -1;
sum = 0;
} else if (i != 0 && initialindex[i - 1] != -1) {
initialindex[i] = initialindex[i - 1];
sum += list[i];
} else {
initialindex[i] = i;
sum = list[i];
}
if (sum > maxsum) {
maxsum = sum;
y = count - 1;
}
printf("Max is: %d and y is:%d", maxsum, y);
printf("%d\n", sum);
}
}

Your code works fine, you just forgot a newline at the end of the first printf so the value of y and sum are concatenated on the terminal:
// original
printf("Max is: %d and y is:%d",maxsum,y);
printf("%d\n",sum);
// corrected
printf("Max is: %d and y is:%d\n",maxsum,y);
printf("Sum is: %d\n",sum);

printf does not output an extra newline and you do not print a space or a newline after y, hence the digits of sum appear immediately after those of y.
Modify the code as:
printf("Max is: %d and y is: %d\n", maxsum, y);
printf("Sum is: %d\n", sum);

Related

I want to print a series of Armstrong numbers which lie between m and n. Here m and n are the two inputs given by the user

I am trying to print the series but whenever I set the range (input given by me) above 407. I only get the output till 407. However, when I set the range below 407 it gives me the result according to the input I have given. Can anybody tell me what I'm doing wrong?
I used an online compiler (www.onlinegdb.com) to write my code.
Here is the code.
#include<stdio.h>
#include<stdlib.h>
int
main ()
{
int m, n;
printf
("Enter two numbers to find the Armstrong numbers that lie between them.\n");
scanf ("%d%d", &m, &n);
system("clear");
if(m>n)
{
m = m + n;
n = m - n;
m = m - n;
}
for (; m < n; m++)
{
int i = m + 1, r, s = 0, t;
t = i;
while (i > 0)
{
r = i % 10;
s = s + (r * r * r);
i = i / 10;
}
if (t == s)
printf ("%d ", t);
}
return 0;
}
enter image description here
enter image description here
Try this code!!!
#include <math.h>
#include <stdio.h>
int main() {
int low, high, number, originalNumber, rem, count = 0;
double result = 0.0;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Armstrong numbers between %d and %d are: ", low, high);
// swap numbers if high < low
if (high < low) {
high += low;
low = high - low;
high -= low;
}
// iterate number from (low + 1) to (high - 1)
// In each iteration, check if number is Armstrong
for (number = low + 1; number < high; ++number) {
originalNumber = number;
// number of digits calculation
while (originalNumber != 0) {
originalNumber /= 10;
++count;
}
originalNumber = number;
// result contains sum of nth power of individual digits
while (originalNumber != 0) {
rem = originalNumber % 10;
result += pow(rem, count);
originalNumber /= 10;
}
// check if number is equal to the sum of nth power of individual digits
if ((int)result == number) {
printf("%d ", number);
}
// resetting the values
count = 0;
result = 0;
}
return 0;
}
Try this code :
#include <stdio.h>
#include <math.h>
int main()
{
int start, end, i, temp1, temp2, remainder, n = 0, result = 0;
printf(“Enter start value and end value : “);
scanf(“%d %d”, &start, &end);
printf(“\nArmstrong numbers between %d an %d are: “, start, end);
for(i = start + 1; i < end; ++i)
{
temp2 = i;
temp1 = i;
while (temp1 != 0)
{
temp1 /= 10;
++n;
}
while (temp2 != 0)
{
remainder = temp2 % 10;
result += pow(remainder, n);
temp2 /= 10;
}
if (result == i) {
printf(“%d “, i);
}
n = 0;
result = 0;
}
printf(“\n”);
return 0;
}

Adding common factors in C

I'm just trying to add all common factors of 3 and 5, stopping at a sum of 1000. I keep getting an expected expression on line 15:18. Is there anyone that can find any new errors or help? It would be much appreciated. Thanks.
#include<stdio.h>
#include<stdlib.h>
/*Multiples of 3 and 5
If we list all natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6
and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.*/
int cd_function(int n, int sum)
{
if(sum >= 1000)
return 0;
if(n%3 == 0 || n%5 == 0)
sum + return cd_function(n, sum);
}
int main(void)
{
int i, iter, sum = 0;
for(i = 0; i < cd_function(iter, sum); i++)
sum++;
return 0;
}
#include<stdio.h>
#include<stdlib.h>
Most people will add a space here:
#include <stdio.h>
#include <stdlib.h>
.
int cd_function(int n, int sum)
{
if(sum >= 1000)
return 0;
if(n%3 == 0 || n%5 == 0)
sum + return cd_function(n, sum);
}
There is two ways, to have this functions add n to sum: Either by passing sum as pointer (reference), this would be done this way:
void cd_function(int n, int * sum) {
// if (*sum >= 1000) - no need to test this here
if (!n%3 || !n%5) {
*sum += stuff;
}
}
or by having the function return the new sum:
int cd_function(int n, int sum) {
// if (sum >= 1000) - no need to test this here
if (!n%3 || !n%5) {
return sum + stuff;
} else {
return sum;
}
}
Now, sum + return cd_function(n, sum); is wrong, YSC already told so in his comment. So you should either use *sum += n; or return sum + n; (i.e. replace stuff by n above).
int main(void)
{
int i, iter, sum = 0;
Since you don't need iter, get rid of it:
int i, sum = 0;
for(i = 0; i < cd_function(iter, sum); i++)
sum++;
a. If you want to sum some[tm] is, then you shouldn't increment sum in each iteration.
for (i = 0; i < cd_function(iter, sum); i++)
;
b. Then you should get a proper exit condition. You wanted to sum everything until you reach a sum >= 1000, so write that in the condition:
for (i = 0; sum < 1000; i++)
/* ??? i < cd_function(iter, sum) */;
c. Depending on the implementation of cd_function you choose from above, you would now either call
cd_function(i, &sum);
or
sum = cd_function(i, sum);
d. And finally, you should add curly brackets, even there is only one statement:
for (i = 0; sum < 1000; i++) {
// option 1
cd_function(i, &sum);
// option 2
sum = cd_function(i, sum);
}
rest is fine:
return 0;
}

Can't print out sociable numbers properly

I have a code that finds the sum of the divisors of a number, but I can't get it to apply on my increasing n and print all the numbers respectively.
The code is
long div(int n) {
long sum = 0;
int square_root = sqrt(n);
for (int i = 1; i <= square_root; i++) {
if (n % i == 0) {
sum += i;
if (i * i != n) {
sum += n / i;
}
}
}
return sum - n;
}
On my main() I need to have a c number that starts from 1 and goes to my MAXCYC which is 28. The n goes from 2 to MAXNUM which is 10000000. The program needs to find all perfect, amicable and sociable numbers and print them with their respective pairs.
Sample output:
Cycle of length 2: 12285 14595 12285
Cycle of length 5: 12496 14288 15472 14536 14264 12496
for (int n = 2; n <= MAXNUM; n++) {
long sum = div(n);
long res = div(sum);
if (res <= MAXNUM) { // Checking if the number is just sociable
int c = 0;
while (c <= MAXCYC && n != res) {
res = div(sum);
c++;
}
if (c <= MAXCYC) {
printf("Cycle of length %d: ", c);
printf("%ld ", sum);
do {
printf("%ld ", res);
res = div(res);
}
while (sum < res);
printf("%ld ", sum);
c += c - 2;
printf("\n");
}
}
}
I only get pairs of cycle length of 1, 2 and nothing above that. Also it doesn't even print it correctly since it says Cycle of length 0: in all of the results without increasing. I think the problem is in the f before the first print but I can't get it to work in a way that as long as my
(n == sum) it prints Cycle of length 1: x x pairs
(n == res && sum < res) it prints Cycle of length 2: x y x pairs
(res <= MAXNUM) it prints Cycle of length c: x y z ... x (c amount of pairs including first x)
What do you guys think I should change?
Ok, this code should work if I understood well your requirement.
#include <stdio.h>
#include <stdlib.h>
int div_sum(int n)
{
long sum = 0;
int square_root = sqrt(n);
for (int i = 1; i <= square_root; i++)
{
if (n % i == 0)
{
sum += i;
if (i * i != n)
{
sum += n / i;
}
}
}
return sum - n;
}
int MAX_N = 10000000;
int MAX_CYCLES = 28;
int main()
{
int cycles;
for(int n = 2; n < MAX_N; n++){
int found = 0;
for(int c = 1; !found && c <= MAX_CYCLES; c++){
cycles = c;
int aliquote = n;
while(cycles--) aliquote = div_sum(aliquote);
//it is a cycle of length c
cycles = c;
if(n == aliquote){
printf("Cycle of length %d: %d", c, n);
while(cycles--){
aliquote = div_sum(aliquote);
printf(" %d", aliquote);
}
printf("\n");
found = 1;
}
}
}
return 0;
}

Fill a defined 2D Array with random numbers in C / Average / Max Value

I've created this 2D 21x21 array that has all it's values set to -1. I wrote it to print the address and value and somehow it only starts at [6][19] why?
What i want to do is to replace some of the -1 values with random numbers from 0 to 100 in the same array. I know i need to seed it with srand but i'm having problems connecting the functions since i'm a total beginner in C.
EDIT 1:
Now i can print the whole array and fill it with random numbers. For the -1 values i just assigned directly which for this case its fine.
What i'm trying now is finding the average of all the values and the maximum number, so what i have is:
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int a[21][21], i , j;
for (i = 0; i < 21; i++)
{
for ( j = 0; j < 21; j++)
{
a[i][j] = GetRand(0, 100);
a[7][15] = -1;
a[10][6] = -1;
a[13][5] = -1;
a[15][17] = -1;
a[17][17] = -1;
a[19][6] = -1;
printf("%3d" , a[i][j]);
}
printf("\n");
}
return 0;
}
// random seed
int GetRand(int min, int max);
int get() {
int i, r;
for (i = 0; i < 21; i++)
{
r = GetRand(0, 100);
printf("Your number is %d \n", r);
}
return(0);
}
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
srand(time(NULL));
Init = 1;
}
rc = (rand() % (max - min +1) +min);
return (rc);
}
// average
int avg()
float sum=0.0;
for(i = 0; i <= 21; i = i + 1) {
for(j = 0; j <= 21; j = j + 1){
sum = sum + a[21][21];
}
printf("The the average number is %.2f\n", sum/21);
}
//find maximum of all values
int *pv = &a[0][0];
max = min = 0;
for (i = 1; i < i*j; ++i){
if (pv[i] > pv[max])
max =i;
if (pv[i] < pv[min])
min = i;
}
printf("The max value is %d in row %d, col %d\n", pv[max], max/j, max%j);
return 0;
}
For the average function the compiler tells me that expected a declaration before i, which is "float sum=0.0;" but i haven't been able to fix that yet.
For the finding the max function i'm not sure yet what i'm doing there, i just have a vague idea of how it's done...am i going in the right direction?
Thanks!
It's very simple: Just assign the result of your GetRand function to the matrix entry.

Code in C won't print out answers?

I am trying to input the value of x and a value for n for the number of terms to find the natural log using Taylor series and another series. The problem is is that my output is not showing up but just showing a blank space when I enter values. Please help!
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(int argc, char **argv){
double x = atof(argv[1]);
double i;
double y;
double result2;
double result;
double error1;
double error2;
double sum;
int n = atof(argv[2]);
if( x <= 0){
printf("Invalid argument\n");
exit (1);
}
if(abs(x-1) <= 1 && abs(x-1) !=0){
for (i = 1; i <= 1; i++){
result -= pow((x-1), i )/ i;
}
}
else{
for(i =1; i <=n; i--){
result += 1/(i * pow((y),i));
}
}
for(i = 0; i <=n; i+=2){
y = (x-1)/(x+1);
sum += pow((y),i) * (1 / (1+i));
result2 = sum * 2 * y;
}
error1 = result - log(x);
error2 = result2 - log(x);
printf("Taylor series: ln(%lf) ~= %lf\n", x, result);
printf(" Error: %lf\n", error1);
printf("Other series: ln(%lf) ~= %lf\n", x, result2);
printf(" Error: %lf\n", error2);
return 0;
}
You are using y without initializing it. Initialize it first before using it in the program. Specifically this line
result += 1/(i * pow((y),i));
will try to use y when it is null.
Also the loop
for (i = 1; i <= 1; i++)
is not that effective as it is iterating only once.

Resources