Question

This code should be in C please!! Your file should meet the following criteria: • Function...

This code should be in C please!!

Your file should meet the following criteria:

• Function prototype and implementation for a calculateDivisors function that takes in an integer parameter, calculates the sum of its divisors, and returns that sum (as an integer value)


• A structure that represents what is represented on each line of output, e.g.,

o Line number

o Sum of the divisors for that line number

o Character array containing “Perfect”, “Deficient”, or “Abundant”

• Pointer declared within the main that will be used to point to an area of memory containing a collection of these structs

• Dynamically allocated memory for the number of structs necessary

• You will assign values to each struct via the pointer

• A loop that goes from X to Y, where X and Y are the numbers inputted by the user at the command line argument

o Note: because the user is entering input here, you will need to check for reasonableness:

▪ The value of X must be greater than or equal to 2

▪ The value of Y must be greater than the value of X

▪ If either one of the above conditions are not met, you should display an error and reprompt.

• The user also specifies the character used for the histogram, which is simply the sum of divisors excluding the original number.

• The line number column uses a column width of 4; the status of divisors is in a column of width 10.

Sample Output:

Prompt % ./lab12.out 15 19 ‘|'

15 is Deficient |||||||||

16 is Deficient |||||||||||||||

17 is Deficient |

18 is Abundant |||||||||||||||||||||

19 is Deficient |

0 0
Add a comment Improve this question Transcribed image text
Answer #1

COde:

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

struct info{
   int lineNo;
   int sum;
   char *msg;
};

int sumOfDivisors(int num)
{
   int i, sum = 0;

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

   return sum;
}

void main(int argc, char* argv[])
{
   int num1,num2, i, p, sumDivisors,j, number = 1;
   struct info *ptr;
   //clrscr();

   if(argc != 4)
   {
       printf("Insufficient arguments!\n");
       return;
   }
   else
   {
       // argv is a character array, convert to int to use the numbers
       // argv[0] contains program name
       num1 = atoi(argv[1]);
       num2 = atoi(argv[2]);

       if(num1 < 2)
       {
           printf("First number should be mroe than 2\n");
           return;
       }

       if(num1 > num2)
       {
           printf("Second number should be more than first number\n");
           return;
       }

       // dynamically allocate memory
       ptr = (struct info *)malloc(sizeof(struct info) * (num2-num1));

       p = 0;       // used in storing data into array of structures
       for(i = num1 ; i <= num2 ; i++)
       {
           sumDivisors = sumOfDivisors(i);
           (ptr + p)->lineNo = number;
           (ptr + p)->sum = sumDivisors;

           if(sumDivisors > i)
               (ptr+p)->msg = "Abundant";
           else if(sumDivisors < i)
               (ptr+p)->msg = "Deficient";
           else
               (ptr+p)->msg = "Perfect";

           number++;
           p++;
       }

           // printing now

       for(i = 0 ; i < p ; i++)
       {
           printf("%d is %s ", num1, (ptr+i)->msg);
           // printing |
           for(j = 0 ; j < (ptr+i)->sum ; j++)
               printf("|");

           printf("\n");
           num1++;
       }
   }
   //getch();
}

Output:

DOSBox 0.74, Cpu speed: max 100% cycles, Frameskip 0, Program: 15 is Def icient 16 is Def icient 17 is Def icient l 18 is Abu

Add a comment
Know the answer?
Add Answer to:
This code should be in C please!! Your file should meet the following criteria: • Function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • need question 3 assap using cin and cout outputs please Ctrl CSC 270 Final Exam-Spring2 1) [10 pts] Create anarray x which includes integers fron ltos obtain the array y which, even formula. Displ...

    need question 3 assap using cin and cout outputs please Ctrl CSC 270 Final Exam-Spring2 1) [10 pts] Create anarray x which includes integers fron ltos obtain the array y which, even formula. Display both arrays x and y in a table with labeled column headings. 0 y- 2x+1 2) 120 pts] Consider the following 2D array. 01 2 -1 X-3 5 0 6 -3 7 -15 Use the standard notation and obtain the following (a) Create and display array...

  • C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

  • I need this code in C++. Should be able to read from a file as stated...

    I need this code in C++. Should be able to read from a file as stated in the directions Exercise #1: Develop a program that prints out the sum of each column of a two-dimensional array The program defines method sumColumn takes a two-dimensional array of integers and returns a single dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and...

  • Please Write the following code in C: PRELAB - Week of October 14 For this prelab...

    Please Write the following code in C: PRELAB - Week of October 14 For this prelab you will implement the following functions: List * initIntegerList() // Return empty list int insertAtHead(int k, List*) // Insert k at head int insertAtTail(int k, List*) // Insert k at tail int removeHead(List *) // Remove head and return its key int getListSize(List *) // Return number of elements in list void printHead(List *) // Print key in head void moveHeadToTail(List *) // Move...

  • C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and...

    C++ Chapter 16 Problem: Implement #25 as a template function (Demonstrate using int, double, string, and x,y pair object) 24. Write a function that searches a numeric array for a specified value. The function should return the subscript of the element containing the value if it is found in the array. If the value is not found, the function should throw an exception. 25. Write a function that dynamically allocates a block of memory and returns a char pointer to...

  • Please Write the task in c++ Task A perfect number is an integer that is equal...

    Please Write the task in c++ Task A perfect number is an integer that is equal to the sum of its divisors (where 1 is considered a divisor). For example, 6 is perfect because its divisors are 1, 2, and 3, and 1 + 2 + 3 is 6. Similarly, 28 is perfect because it equals 1 + 2 + 4 + 7 + 14. A quite good number is an integer whose badness—the size of the difference between the...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Code in C++. Can someone make it so that the code below can be compiled? ▪...

    Code in C++. Can someone make it so that the code below can be compiled? ▪ Creating an empty queue ▪ Inserting a value ▪ Removing a value ▪ Finding the size of the queue ▪ Printing the contents of the queue ▪ Adding the contents of one queue to the end of another ▪ Merging the contents of two queues into a third, new, queue Class Attributes Your class should be implemented using a linked list and should have...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • In basic c develop the code below: (a) You will write a program that will do...

    In basic c develop the code below: (a) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter ‘X’ You will compute statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character ‘X’. Example input mJ0*5/]+x1@3qcxX The ‘X’ should not be included when computing the statistics...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT