?
Solved

lab9.cpp-code check

Posted on 2005-03-09
1
Medium Priority
?
277 Views
Last Modified: 2010-04-01

// I am having problems compiling this program can you please help me

#include <iostream>
#include <string.h>

using std::cout;
using std::cin;
using std::endl;


// fill array of size s with unique random integers
void fillUniq( int * , int s );  
// display all integers in an array of size s
void display( int * , int s );
// return true if key exists in array
bool found( int * , int key, int size);
   /* ptr+i will ensure that each time in the loop the pointer points to the
   next array slot
   ptr+i must be in () to be evaluated before the dereference operator *
   ptr+i means that ptr is incremented by i * size of int which is i * 4 bytes
  */


  void bubbleSort (char * [], const int );
  void swap ( char * , char * );

 
int size = 25;


int main ()
{
char *s1;
char *s2;
char *s3;

 const int arraysize = 25;
 char *words[5];
 for (int i=0; i < arraysize; i++)
      words[i] = new char[size];
     

cout << "data in original order\n";

for ( int i = 0; i < 5; i++)
 cin >> words[i];

 bubbleSort ( words, arraysize);

 cout << " data items in ascending order\n";

 for ( int j = 0; j < arraysize; j++)
   cout << words[j] << endl;

cout << "s1 = " << s1 << "\ns2 = " << s2
     << strcmp (s1, s2 );
strcpy (s1, s2);

cout << "The string in array s2 is:  " << s2
     << "\nThe string in array y is:  " << s1 << '\n';
   cout << endl;



 
/*************************************************************************************
            PROBLEM #2
***********************************************************************************/
  int b[25];
  int *bPtr = b;
  int size = 100;
 
  cout << "Array b printed with:\n"
       << "Array subscript notation\n";
 
  fillUniq( bPtr , int size );
  display( bPtr , int size );
 
return 0;
}
 

void fillUniq(int *Num , int size)
 {

   for ( int i = 0; i < size; i++)
     if((int * Num, int key, int size))
   
    return true;
    return false;
 }
   
 
 
 
 bool found( int * ptr, int key, int size)
 {
      for (int i=0; i <size; i++ )
      if ( *(ptr+i) == key )
        return true;
      return false;
 }

 void display( int *ptr , int s )
 {
   for ( int i = 0; i < size; i++)
    {
      cout << ptr[i] << endl;
    }
}

void bubbleSort ( char * array[], const int size)
{
for ( int a = 0; a < size - 1; a++)
  if ( array [a] > array [a + 1 ] )
    swap (array [ a], array [a + 1]);
 }

 void swap ( char * element1Ptr, char * element2Ptr)
 {
   char *hold = element1Ptr;
   element1Ptr = element2Ptr;
   element2Ptr = hold;
 }

0
Comment
Question by:Yesenia1
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13502255
The following will compile:

// I am having problems compiling this program can you please help me

#include <iostream>
#include <string.h>
#include <time.h>
#include <stdlib.h>

using std::cout;
using std::cin;
using std::endl;


// fill array of size s with unique random integers
void fillUniq( int * , int s );  
// display all integers in an array of size s
void display( int * , int s );
// return true if key exists in array
bool found( int * , int key, int size);
   /* ptr+i will ensure that each time in the loop the pointer points to the
   next array slot
   ptr+i must be in () to be evaluated before the dereference operator *
  ptr+i means that ptr is incremented by i * size of int which is i * 4 bytes
 */


  void bubbleSort (char * [], const int );
 void swap ( char * , char * );

 
int size = 25;


int main ()
{
char *s1;
char *s2;
char *s3;

const int arraysize = 25;
 char *words[5];
for (int i=0; i < arraysize; i++)
     words[i] = new char[size];
     

cout << "data in original order\n";

for ( i = 0; i < 5; i++)
cin >> words[i];

bubbleSort ( words, arraysize);

cout << " data items in ascending order\n";

for ( int j = 0; j < arraysize; j++)
  cout << words[j] << endl;

cout << "s1 = " << s1 << "\ns2 = " << s2
    << strcmp (s1, s2 );
strcpy (s1, s2);

cout << "The string in array s2 is:  " << s2
    << "\nThe string in array y is:  " << s1 << '\n';
  cout << endl;




/*************************************************************************************
         PROBLEM #2
***********************************************************************************/
  int b[25];
 int *bPtr = b;
 int size = 100;
 
 cout << "Array b printed with:\n"
      << "Array subscript notation\n";
 
 fillUniq( bPtr , size );
  display( bPtr , size );
 
return 0;
}
 

void fillUniq(int *Num , int size)
{

  srand(time(NULL));
  for ( int i = 0; i < size; i++) Num[i] = rand();
}
   
 
 
 
bool found( int * ptr, int key, int size)
{
     for (int i=0; i <size; i++ )
    if ( *(ptr+i) == key )
      return true;
     return false;
}

void display( int *ptr , int s )
{
  for ( int i = 0; i < size; i++)
   {
     cout << ptr[i] << endl;
   }
}

void bubbleSort ( char * array[], const int size)
{
for ( int a = 0; a < size - 1; a++)
 if ( array [a] > array [a + 1 ] )
   swap (array [ a], array [a + 1]);
}

void swap ( char * element1Ptr, char * element2Ptr)
 {
  char *hold = element1Ptr;
  element1Ptr = element2Ptr;
  element2Ptr = hold;
}


You were multiply declaring a couple of variables - and, why did't you try to provide a reasonable implementation for 'fillUnique()'? What still remains to be resolved is using 's1', 's2' in 'main()' as unitialized variables/pointers. What purpose do they serve?
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question