What is wrong with my C code?

My program will crash and I can't seem to figure out what I did wrong. The code is attached. My goal here is to list all of those factors that I have added to the array I created. Then at the end of the program I need to print each factor, one by one.

I am using the Dev C++ compiler. Thank you very much
/*
John Doe
COP 3223-0001 LEC (16050)
Assignment #2 Aid to an age calculator
Due: February 12, 2009
This program is designed to calculate how much you owe
on your student loan after each monthly payment.
*/

//load library that contains printf function
#include <stdio.h>

int main(void)
{
    int posInt;
    int i, j, k, sum, product;
    
    //Gather user input
    again:
    printf("Enter a positive integer greater than 1. (Ex 1, 4, 100)\n");
    scanf("%d", &posInt);

    //conditional statement testing if positive integer
    if(posInt > 0)
    {
         //run function to calc factors
         printf("Good works\n");
    }
    else if(posInt <= 0)
    {
         printf("Please enter a valid positive integer!\n");
         goto again;
    } 
    
    int factors[posInt];
    
    j = 0;
    sum = 0;
    product = 0;
    
    for(i=0; i <= posInt; i++)
    {
         if(posInt % i == 0)
         {
              factors[j] = i;
              sum += i;
              product *= i;
              j++;
         }
    }
    
    printf("Here are a list of positive factors of %d:\n", posInt);
         
    for(k=0; k <= j; k++)
    {
         printf("%d ", factors[k]);
    }
    
    //print sum and product of factors
    printf("The number of positive factors of %d is %d.\n", posInt, j);
    printf("Here is the sum of the factors of %d: %d\n", posInt, product);
    printf("Here is the product of the factors of %d: %d\n", posInt, product);
    
    system("pause");
    return 0;
}

Open in new window

LVL 1
rootjdAsked:
Who is Participating?
 
mr_egyptianConnect With a Mentor Commented:
Since this was obviously a homework problem, I didn't give an outright answer, although I did state, indirectly, what the main issue was, which is a divide by zero via the modulus operator.  It's only a 50 point question, but a little credit where credit is due...
0
 
mr_egyptianCommented:
Think about how the modulus operator works, and then think about dividing a number by zero.
0
 
mr_egyptianCommented:
Think about how the modulus operator works, and then think about dividing a number by zero.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
masheikCommented:
hi,

  In your code you are using system("pause");
 
  so that you need to add a header file,

#include <windows.h>

Open in new window

0
 
masheikCommented:
In C you can not able to give the array size at run time

int factors[posInt];

insted you can declare int factors[100] ;

something like this,

and the problem in modulus operator this is your major probelm
and in for loop check that out find out the answer

hope this helps
0
 
ozoCommented:
   for(k=0; k <= j; k++)
    {
         printf("%d ", factors[k]);
    }
when k==j, you print an element of  factors that has never been set


when you start with product = 0, product *= i; will never change it


in
  printf("Here is the sum of the factors of %d: %d\n", posInt, product);
the text does not describe what you are printing


if you enter too large a number,
      int factors[posInt];
will fail
0
 
Infinity08Commented:
>> In your code you are using system("pause");
>> 
>> so that you need to add a header file,
>> 
>> #include <windows.h>

The system function is a standard C function and is in the standard <stdlib.h> header. No need to include <windows.h> or any other platform dependent headers.
0
 
rootjdAuthor Commented:
I'm almost sure the problem lies while I'm trying to assign these values to the array. I ran a debug and found it made it past the part where I initialized the array     int factors[posInt];

It closes unexpectedly only when assigning the array values for valid factors. The % (modulus) is being used correctly.

0
 
rootjdAuthor Commented:
Well I changed the i variable from i=0 to i=1 and now it's working!
0
 
mr_egyptianCommented:
That's because i % 0 is the equivalent of a division by zero.
0
 
ozoCommented:
The result of the / operator is the quotient from the division of the first operand by the
second; the result of the % operator is the remainder. In both operations, if the value of
the second operand is zero, the behavior is undefined.


undefined behavior
behavior, upon use of a nonportable or erroneous program construct or of erroneous data,
for which this International Standard imposes no requirements
NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable
results, to behaving during translation or program execution in a documented manner characteristic of the
environment (with or without the issuance of a diagnostic message), to terminating a translation or
execution (with the issuance of a diagnostic message).

0
 
CSecurityCommented:
I changed a little and result: 0 error(s), 0 warning(s)
I changed you code in this way::
added #include <windows.h> in first
and changed
int factors[posInt];
to
int *factors = (int*)malloc(posInt);

and then added
free(factors);
before return.
now, your code works well :D

regards...
#include <stdio.h>
#include <windows.h>
 
int main(void)
{
    int posInt;
    int i, j, k, sum, product;
    
    //Gather user input
    again:
    printf("Enter a positive integer greater than 1. (Ex 1, 4, 100)\n");
    scanf("%d", &posInt);
 
    //conditional statement testing if positive integer
    if(posInt > 0)
    {
         //run function to calc factors
         printf("Good works\n");
    }
    else if(posInt <= 0)
    {
         printf("Please enter a valid positive integer!\n");
         goto again;
    } 
    
    int *factors = (int*)malloc(posInt);
    
    j = 0;
    sum = 0;
    product = 0;
    
    for(i=0; i <= posInt; i++)
    {
         if(posInt % i == 0)
         {
              factors[j] = i;
              sum += i;
              product *= i;
              j++;
         }
    }
    
    printf("Here are a list of positive factors of %d:\n", posInt);
         
    for(k=0; k <= j; k++)
    {
         printf("%d ", factors[k]);
    }
    
    //print sum and product of factors
    printf("The number of positive factors of %d is %d.\n", posInt, j);
    printf("Here is the sum of the factors of %d: %d\n", posInt, product);
    printf("Here is the product of the factors of %d: %d\n", posInt, product);
    
    system("pause");
    free(factors);
    return 0;
}

Open in new window

0
 
CSecurityCommented:
sorry, I found out later that you have runtime errors too, then i resolved them all and now works well
now your app works well
regards...
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
 
int main(void)
{
    int posInt;
    int i, j, k, sum, product;
    
    //Gather user input
    again:
    printf("Enter a positive integer greater than 1. (Ex 1, 4, 100)\n");
    scanf("%d", &posInt);
 
    //conditional statement testing if positive integer
    if(posInt > 0)
    {
         //run function to calc factors
         printf("Good works\n");
    }
    else if(posInt <= 0)
    {
         printf("Please enter a valid positive integer!\n");
         goto again;
    } 
    
    int *factors = (int*)malloc(posInt);
    
    j = 0;
    sum = 0;
    product = 1;
 
    for(i=1; i <= posInt; i++)
    {
         if(posInt % i == 0)
         {
              factors[j] = i;
              sum += i;
              product *= i;
              j++;
         }
    }
    
    printf("Here are a list of positive factors of %d:\n", j);
 
    for(k=0; k < j; k++)
    {
         printf("%d ", factors[k]);
    }
 
    //print sum and product of factors
    printf("\nThe number of positive factors of %d is %d.\n", posInt, j);
    printf("Here is the sum of the factors of %d: %d\n", posInt, sum);
    printf("Here is the product of the factors of %d: %d\n", posInt, product);
 
    printf("Press any key to continue . . .");
    getch();
    free(factors);
    return 0;
}

Open in new window

works.JPG
0
All Courses

From novice to tech pro — start learning today.