?
Solved

lab9.cpp-code check

Posted on 2005-03-09
1
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

765 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