6 Replies - 212 Views - Last Post: 10 February 2014 - 09:22 PM Rate Topic: -----

#1 damatuse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Help in abundant number program

Posted 10 February 2014 - 06:24 PM

I have tried and tried and tried and am stuck and am not getting the correct output. I have to write a program to scan in a number, which is the number of times i will test if a number is abundant, and then based on that number I input that amount of numbers. Here are the directions:

Input Specification
1. The first integer input will be a positive integer, n, indicating the number of test cases coming next.
2. The next n inputs are single positive integers each, and for each you are to determine whether the number is abundant or not.

Output Specification
Output a line with one of the two the following formats for each input number:

Test case #t: X is abundant.
Test case #t: X is NOT abundant.
where t represents the number of the test case (starting with 1), and X represents the number for that case.

Can someone help me or guide me in the correct direction. Thanks!

#include <stdlib.h>
#include <stdio.h>

int main(void) {

    int i;
    int SIZE;
    int num[SIZE];
    int sum = 0;

    scanf("%d", &SIZE);
    scanf("%d", &num[SIZE]);

    for(i=1;i<=SIZE;i++) {
        if((num[SIZE]%i) == 0)
            sum += 1;

    if (sum > num[SIZE])
        printf("Test case #%d: %d is abundant.\n", i, num[SIZE]);
    else (sum <= num[SIZE]);
        printf("Test case #%d: %d is NOT abundant.\n", i, num[SIZE]);

    }
    system("PAUSE");
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help in abundant number program

#2 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 64
  • Joined: 02-February 14

Re: Help in abundant number program

Posted 10 February 2014 - 06:43 PM

I'm a little confused as to what you're trying to do. By abundant do you just mean populated?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Help in abundant number program

Posted 10 February 2014 - 06:55 PM

There are several things wrong with your program. Let's start with the following snippet:
    int SIZE;
    int num[SIZE]; // This is a Variable Length Array (VLA)

There is at least one thing wrong, probably two, with the above small snippet. First you're using SIZE before you initialize it to a known value, it could be almost any value. Second, unless you're using a C99 compiler (and maybe a C11 compiler) array sizes in C must be compile time constants.

I really recommend you not use the VLA "feature", since only the C99 standard guarantees that this feature is available. Prior standards don't allow them and the current standard makes their availability optional. If you must have a user defined array size use malloc/free since that is available in all the C standards.

Also look at this snippet:
    for(i=1;i<=SIZE;i++)

Remember array in C/C++ start at zero and end at SIZE - 1. You appear to be trying to iterate over your array starting at one and stopping at SIZE, this can often cause a program to crash.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 damatuse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Re: Help in abundant number program

Posted 10 February 2014 - 08:42 PM

This is what I have re-wrote it to; The input is 3 (for the number of times the program needs to be compiled) 6 12 17; and the output is:
Test case #1: 3 is NOT abundant.
Test case #2: 3 is NOT abundant.
Test case #3: 3 is NOT abundant.

I am confused as to how the program is suppose to read the numbers after 3 and put them in the spot of 3 instead of 3.

#include <stdlib.h>
#include <stdio.h>

int main(void) {

    int i;
    int num;
    int sum = 0;

    scanf("%d", &num);

    for(i=1;i<=num;i++) {
        if((num%i) == 0)
            sum += 1;

    if (sum > num)
        printf("Test case #%d: %d is abundant.\n", i, num);
    else (sum <= num);
        printf("Test case #%d: %d is NOT abundant.\n", i, num);

    }
    system("PAUSE");
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,725
  • Joined: 05-May 12

Re: Help in abundant number program

Posted 10 February 2014 - 08:55 PM

Have you covered arrays and/or functions in class? Are you allowed to use them in your code?
Was This Post Helpful? 0
  • +
  • -

#6 damatuse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 14

Re: Help in abundant number program

Posted 10 February 2014 - 09:00 PM

Yes, we have learned arrays and functions in class and I keep reading over my notes and power points but I can't seem to figure out how to implement it in this situation without giving the array a certain number. I need the user to be able to input a number which will be the amount of numbers tested and then followed by that amount of numbers.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,725
  • Joined: 05-May 12

Re: Help in abundant number program

Posted 10 February 2014 - 09:22 PM

You could either just allocate a big array and make sure that users don't enter more than number of elements; or
You could dynamically allocate an array that is sized just right for the number the inputs that the user intends to enter.

In either case, you then do a loop and input the numbers and store them in the array. Then next you loop and call your function to check for abundance.

An alternative is simply loop the number of times the number of inputs the user intends to enter. Then within your loop, input the number, and check for abundance. This solution requires neither arrays nor functions, although moving the code to check for abundance into a function would make the code more readable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1