Link to home
Start Free TrialLog in
Avatar of dazedandconfused69
dazedandconfused69

asked on

linker exercise - cannot get started

Hi,
This is my first posting.  This is  HW assignment and I am not asking for the outright answer. Yes, I know the experts could not do that anyway according to the rules.  I really want to understand this.  I'm at a loss at where to start.  The first hint is "what does p1.c need that is does not define?"  

I don't know.  My best guess is :

/*  PURPOSE:  To:
    (1) create an array of n integers with calloc(n,sizeof(int))
    (2) initialize this array with random integers with (rand() % 256)
    (3) call createAndCount() and store the returned sum
    (4) free() the array
    (5) return the sum
 */

I don't see anything in the existing code that would do those 5 items.  

Also, this is probably for later, but I'm not sure how this links with the other program (the whole purpose of the exercise).  But I will post a separate question later once I get a decent start going.  

Also, I have no idea how to award points - what is considered 500 points or 250 points.  
The program file p1.c below compiles under Linux to create an object file p1.o. It is to be linked with another file p2.o to create a running program, whole. It just adds two numbers.

/* p1.c
 */
#include <stdlib.h>
#include <stdio.h>


/**** Declarations go here: ****/

#define		STRING_LEN	256

/*  PURPOSE:  To:
    (1) create an array of n integers with calloc(n,sizeof(int))
    (2) initialize this array with random integers with (rand() % 256)
    (3) call createAndCount() and store the returned sum
    (4) free() the array
    (5) return the sum
 */
int		createAndCount	();



/**** Function and variables go here: ****/

/*  PURPOSE:  To keep track of the length of an array.
 */
int		n;



/*  PURPOSE:  To compute and return an arbitrary function from an array 'array'
 *      of 'n' integers.
 */
int		count	(int*	array)
{
  int	sum	= 0;
  int	i;

  for  (i = 0;  i < n-1;  i++)
    sum += array[i+1] - array[i];

  return(sum);
}



/*  PURPOSE:  To harass CSC 374 students.  Ignores parameters.  Returns
 *      'EXIT_SUCCESS' to OS.
 */
int	main	()
{
  char	text[STRING_LEN];

  do
  {
    printf("Please enter a positive integer: ");
    fgets(text,STRING_LEN,stdin);
    n = atoi(text);
  }
  while  (n <= 0);

  printf("The value is %d\n",createAndCount());
  return(EXIT_SUCCESS);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dazedandconfused69
dazedandconfused69

ASKER

Thanks!  It's a bit late where I am but I will try this solution tomorrow morning if I can't figure it out now and will respond as soon as I try it.  
Thanks, this definitely got me starting.  This is my first class using C and I only have a limited java background so this is all very difficult for me.  I understand I'm supposed to define createandcount in a separate file and I get how to link those two at the end once I've written both of them.  Here are my prof's hints:

    (1) create an array of n integers with calloc(n,sizeof(int))
    (2) initialize this array with random integers with (rand() % 256)
    (3) call createAndCount() and store the returned sum
    (4) free() the array
    (5) return the sum

So starting with 1, I have questions:

(note: I am continuing your naming convention of add2numbers.c for the main program and createAndCount.c for the side program to be linked to the main program.

(a) is the array creation done inside the function createAndCount or in the add2numbers program?
(b) I researched calloc and if I understand correctly, the code I need should be something like:

      int* array;
      int NumElements = 1000;
      int SizeOfEachElement = sizeof(int);
      array = calloc(NumElements, SizeOfEachElement);


I have been reading up on calloc online and it is not clear to me.  This is also my first class with pointers and I won't claim I get them strongly either.  What kind of definition of createAndCount is expected in the add2numbers.c file?  I thought the point of linking was that I would define the function createAndCount in another file.  

Also, is there a FAQ on points?  I want to properly as possible award the correct amount of points.  
>> Thanks, this definitely got me starting.  This is my first class using C and I only have a limited java background so this is all very difficult for me.
   Glad to have helped you get started. No sense being hung up on how to build a project consisting of two or more .c files.
   Believe me that your limited java background will help you in C (although you will end up probably writing java code at times; but the compiler will guide you back to C - I've made that mistake a number of times myself).
   Learning C is difficult for everyone, so you are not alone. One language that is 10 times harder than C is C++ (although you can get by profesionally with only it being 4 times harder).

>> Also, is there a FAQ on points?  I want to properly as possible award the correct amount of points.
   A fair question. For limited EE members, points cost $$, so use them wisely. For premium members, there are unlimited points so less wisdom needs to be applied.

   https://www.experts-exchange.com/help.jsp?hi=23#hs=8&hi=102

 I've looked up some additional FAQs for you that I think will be useful:

   https://www.experts-exchange.com/help.jsp?hi=23#hs=8&hi=99

   https://www.experts-exchange.com/help.jsp?hi=23#hs=8&hi=101

   https://www.experts-exchange.com/help.jsp?hi=23#hs=8&hi=504

   https://www.experts-exchange.com/help.jsp?hi=23  

   https://www.experts-exchange.com/questions/25157027/21-Feb-10-16-Where-is-link-to-EE-Grading-Policy.html

   https://www.experts-exchange.com/help.jsp#hs=29&hi=403
   
Do I need an extern declaration for this createAndCount function in add2numbers.c?  Or when I do the gcc that combines them both, does that do the equivalent of an extern?  I have never written a C declaration before so am confused about how to link add2numbers.c to createAndCount.c.