Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

// lab 4.3.cpp : Defines the entry point for the console application. // #includ

ID: 3621650 • Letter: #

Question

// lab 4.3.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include "stdafx.h"
#include "stdlib.h"
# include "assert.h"

int main ( int argc , char ** argv ) {
int i;

/* bits to sort specified as an integer */
int m = atoi (argv [1]);
/* number of bits */
int n = 32;
/* allocate memory for a pointer p */
int **p = ( int **) malloc (n* sizeof ( int *));
/* p is a pointer to an array of n pointers to integers */
for (i = 0; i < n; i ++) {
/* allocate memory for each integer */
p[i] = ( int *) malloc ( sizeof ( int ));
}/* set each integer to be the corresponding bit of m */
for (i = 0; i < n; i ++) {
*p[i] = (m >> i) & 1;
}
for (i = 0; i < n; i ++) {
printf ("%d", *p[i]);
}
printf (" ");
/* sort the integers */
sort (p, n);
for (i = 0; i < n; i ++) {
printf ("%d", *p[i ]);
}
printf (" ");
/* free memory */
for (i = 0; i < n; i ++) {
free (p[i ]);
}
free (p);
}

void merge ( int **p, int n) {
printf (" merge %d ", n);
if (n == 2) {
/* base case */
sort2 (p[0] , p [1]);
} else {
int i, j, k;
/* allocate memory */
int ** q1 = ( int **) malloc ((n /2)* sizeof ( int *));
int ** q2 = ( int **) malloc ((n /2)* sizeof ( int *));
/* tease apart even and odd subsequences */
j = k = 0;
for (i = 0; i < n; i ++) {
if (i < n /2) {
/* 1st subsequence */
if (i % 2 == 0) {
q1[j ++] = p[i]; /* even of 1st */
} else {
q2[k ++] = p[i]; /* odd of 1st */
}
} else {
/* 2nd subsequence */
if (i % 2 == 0) {
q2[k ++] = p[i]; /* even of 2nd */
} else {
q1[j ++] = p[i]; /* odd of 2nd */
}
}
}
/* produce two balanced sorted sequences */
merge (q1 , n /2);
merge (q2 , n /2);
/* merge the two balanced sequences */
for (i = 0; i < n; i += 2) {
sort2 (p[i], p[i +1]);
}
/* free memory */
free (q1 );
free (q2 );
}
}

void sort (int **p,int n){
merge(p,n);
}

void sort2 (int *c, int *d){
int mid;
if (c > d) {
mid = *c;
*c = *d;
*d = mid;
}
else{
*c = *c;
*d = *d;
}
}

This is my code. However, it said:
1>c:usersjiedocuments isual studio 2008projectsoh my godoh my godoh my god.cpp(31) : error C3861: 'sort': identifier not found
1>c:usersjiedocuments isual studio 2008projectsoh my godoh my godoh my god.cpp(47) : error C3861: 'sort2': identifier not found
1>c:usersjiedocuments isual studio 2008projectsoh my godoh my godoh my god.cpp(77) : error C3861: 'sort2': identifier not found
1>Build log was saved at "file://c:UsersJieDocumentsVisual Studio 2008Projectsoh my godoh my godDebugBuildLog.htm"
1>oh my god - 3 error(s), 0 warning(s)

Why? i defined the functions. Why do those errors occur?

Explanation / Answer

You simply forgot to declare the function prototypes before main(). If you write your functions after main(), you have to delcare the prototypes so that the code in main() can know that it exists.

Just copy the function header of each function and paste it before main with a semicolon, for example

// lab 4.3.cpp : Defines the entry point for the console application.
//

#include


#include "stdafx.h"
#include "stdlib.h"
#include "assert.h"

void sort (int **p,int n);
void sort2 (int *c, int *d);

main(............etc.