Difference between revisions of "CSC231 Final Exam Fall 2017"

From dftwiki3
Jump to: navigation, search
Line 110: Line 110:
 
<br />
 
<br />
 
<br />
 
<br />
 +
=Problem 6: Linked Lists in C=
 +
<br />
 +
Your assignment is to complete the function ''makeList()'' in the program below, so that it outputs the correct information.  Several examples of how the finished program should work are given below.
 +
<br />
 +
==Incomplete Program==
 +
<br />
 +
::<source lang="C">
 +
// makeList.c
 +
// D. Thiebaut
 +
// program to complete for CSC231, Final Exam
 +
// when the function makeList() is written correctly,
 +
// the program outputs:
 +
// 1
 +
// 2
 +
// -5
 +
// 10
 +
// 20
 +
// -30
 +
// 5
 +
//
 +
#include <stdio.h>
 +
#include <stdlib.h>
 +
#include <ctype.h>
 +
 +
struct node {
 +
  int item;
 +
  struct node* next;
 +
};
 +
typedef struct node node_t;
 +
 +
 +
//--------------------------------------------------
 +
// makeList( array, arraySize ): takes the contents
 +
// of the array and stores it, in the same order, in
 +
// a singly linked list.
 +
//--------------------------------------------------
 +
node_t *makeList( int* table, int n ) {
 +
 
 +
  // add your code here...
 +
 +
  return NULL;  // you probably want to remove this line!
 +
}
 +
 +
//--------------------------------------------------
 +
int main( int argc, char *argv[] ) {
 +
  int i, x, A[] = { 1, 2, -5, 10, 20, -30, 5 };
 +
  int *B;
 +
 +
  //--- make a linked list with A and print it in same order ---
 +
  node_t *p, *head = makeList( A, sizeof(A)/sizeof(int) );
 +
 
 +
  for ( p=head; p!=NULL; p=p->next ) {
 +
    printf( "%d\n", p->item );
 +
  }
 +
 +
 +
  //--- if user entered ints on the command line, put them ---
 +
  //--- in array B and put B in linked list.  Then print  ---
 +
  //--- linked list.                                      ---
 +
  if ( argc > 1 ) {
 +
    printf( "\n" );
 +
 +
    //--- create array B with same number of cells as ints on command line---
 +
    B = (int *) malloc( (argc-1) * sizeof( int ) );
 +
 +
    //--- convert strings taken from command line in true integers ---
 +
    for ( i=1; i<argc; i++ )  {
 +
      B[i-1] = atoi( argv[i] );
 +
      //printf( "B[%d] = %d\n", i-1, B[i-1] );
 +
    }
 +
 +
    //--- make linked list out of B, and print linked list ---
 +
    head = makeList( B, argc-1 );
 +
    for ( p=head; p!=NULL; p=p->next ) {
 +
      printf( "%d\n", p->item );
 +
    } 
 +
  }
 +
}
 +
</source>
 +
<br />
 +
==Examples==
 +
<br />
 +
::<source lang="text">
 +
cs231a@aurora ~/HWs/FINAL/PB6 $ gcc -o makeList makeList.c
 +
cs231a@aurora ~/HWs/FINAL/PB6 $ ./makeList
 +
1
 +
2
 +
-5
 +
10
 +
20
 +
-30
 +
5
 +
cs231a@aurora ~/HWs/FINAL/PB6 $ ./makeList 100 200 300 1000
 +
1
 +
2
 +
-5
 +
10
 +
20
 +
-30
 +
5
 +
 +
100
 +
200
 +
300
 +
1000
 +
cs231a@aurora ~/HWs/FINAL/PB6 $ ./makeList 100
 +
1
 +
2
 +
-5
 +
10
 +
20
 +
-30
 +
5
 +
 +
100
 +
cs231a@aurora ~/HWs/FINAL/PB6 $ ./makeList hello there
 +
1
 +
2
 +
-5
 +
10
 +
20
 +
-30
 +
5
 +
 +
0
 +
0
 +
 +
</source>
 +
<br />
 +
==Submission==
 +
<br />
 +
* Submit your code on Moodle, in the Problem 6 section.
 +
<br />
 +
<!-- ====================================================================== -->
 +
<!-- ====================================================================== -->
 +
<!-- ====================================================================== -->
 +
 
=Solutions=
 
=Solutions=
 
==Bash Script==
 
==Bash Script==

Revision as of 13:29, 12 December 2017


...