GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Oct-2005, 19:33
Cat Cat is offline
New Member
 
Join Date: Oct 2005
Posts: 11
Cat is on a distinguished road

Array printing


Need help fixing the code to make this program output all the array! Help would be really nice!

CPP / C++ / C Code:
#include <iostream.h>
#include <math.h>
#define SIZE 20

int get_array (int original_array[], int number_used);
void display_array (int original_array [], int number_used);
int reverse_array (int original_array [],int number_used);
void find_max (int original_array [], int number_used);
void find_min (int original_array [], int number_used);
void sort_array (int original_array [], int number_used);
void smallest_spread (int original_array [], int
number_used);
void largest_spread (int original_array [], int
number_used);
void positive_negative (int original_array [],int
positive_array [], int negative_array []);
void swap_values (int& v1, int& v2);
int index_of_smallest (const int original_array [], int
start_index, int number_used);
int number_used;
int i;

void main()
{
    int original_array [SIZE];
    int positive_array [SIZE];
    int negative_array [SIZE];
    get_array (original_array,number_used);
    cout<<"Original array: ";
    display_array (original_array,number_used);
    cout<<endl<<"Reversed array: ";
    reverse_array (original_array,number_used);
    cout<<endl<<"Maximum value = ";
    find_max (original_array,number_used);
    cout<<endl<<"Minimum value = ";
    find_min (original_array,number_used);
    cout<<endl<<"Sorted array: ";
    sort_array (original_array, number_used);
    display_array (original_array,number_used);
    cout<<endl<<"Smallest spread = ";
    smallest_spread (original_array,number_used);
    cout<<endl<<"Largest spread = ";
    largest_spread (original_array,number_used);
    positive_negative
(original_array,positive_array,negative_array);
    cout<<endl<<" Positive array: ";
    display_array (original_array,number_used);
    cout<<endl<<"Negative array: ";
    display_array (original_array,number_used);
    cout<<endl;
}
int get_array (int original_array[], int number_used)
{
    cout<<"Enter the array size: ";
    cin>> number_used;
        int i = 0;
        while(number_used>0)
        {
            cout<<"Enter element # "<<i +1<<": ";
            cin>> original_array [i];
            i++;
            number_used --;
        }
 return (original_array [i] , number_used);
}
void display_array (int original_array [],int number_used)
{
    for(int i=0; i<number_used;i++)
        cout<< original_array[i];
    cout<<endl;

}
int reverse_array (int original_array [],int number_used)
{
   for (int i=number_used; i>=0; i--)
   {
    return (original_array [i]);   
   }
}
void find_max (int original_array [], int number_used)
{
    int max;
    int i;
  max = original_array [0];
  for(i=1;i<number_used;i++)
  {
   if(original_array [i] > max)
    max = original_array [i];
  }
  cout<<max;
}
void find_min (int original_array [], int number_used)
{
    int min;
    int i;
  min = original_array [0];
  for(i=1;i<number_used;i++)
  {
   if(original_array [i] < min)
    min = original_array[i];
  }
    cout<<min;
}
void sort_array (int original_array [], int number_used)
{
int index_of_next_smallest;
  
 for (int index = 0; index < number_used - 1; index ++)
 {
  index_of_next_smallest = index_of_smallest
(original_array, index, number_used);
  swap_values (original_array [index],
original_array[index_of_next_smallest]);
 }
}
void smallest_spread (int original_array [], int
number_used)
{
    int i=0;
    int j=1;
    int smallest;
    int sum;
 smallest = original_array [i] - original_array [j];
 do
 {
     sum = original_array [i] - original_array [j];
     if(smallest > sum)  
         smallest = sum;
     i++;
     j++;
 }
 while(number_used >= i-1);
 cout<<fabs (smallest);
}
void largest_spread (int original_array [], int number_used)
{
    int i=0;
    int j=1;
    int largest;
    int sum;
    largest = original_array [i] - original_array [j];
    do
    {
     sum = original_array [i] - original_array [j];
     if(largest < sum)  
         largest = sum;
     i++;
     j++;
 }
 while(number_used >= i-1);
 cout<< fabs (largest);
}
void positive_negative (int original_array [],int
positive_array [], int negative_array [])
{
 int i;
 int count;
    while (count >=0)
    {
        if(original_array >= 0)
        {
            positive_array [i] = original_array [i];
            cout<< positive_array [i];
        }
        else
        {
            negative_array [i] = original_array [i];
            cout<<negative_array;
        }
            //return (positive_array [i], negative_array [i]);
    }
}
void swap_values (int& v1, int& v2)
    {
        int temp;
        temp = v1;
        v1 = v2;
        v2 = temp;
    }
    int index_of_smallest (const int original_array [], int
start_index, int number_used)
    {
        int min = original_array [start_index],
                   index_of_min = start_index;
        for (int index = start_index + 1; index <
number_used; 
index ++)
            if (original_array [index] < min)
            {
                min = original_array [index];
                index_of_min = index;
            }
        return index_of_min;
    }
Last edited by LuciWiz : 27-Oct-2005 at 03:23. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 26-Oct-2005, 19:40
Cat Cat is offline
New Member
 
Join Date: Oct 2005
Posts: 11
Cat is on a distinguished road

Array problem


[Merged thread]
Duplicate thread merged (not deleted) to preserve relevant answer(s). See bellow.
Last edited by LuciWiz : 27-Oct-2005 at 03:30. Reason: Please don't double-post
  #3  
Old 26-Oct-2005, 22:24
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Thumbs up

Re: Array problem


Hi Cat,

Welcome to the GID Forums.
Did you read the guidelines??
Please insert the C++ code between [c++] and [/c++] tags.
And Dont post the same topics in different threads.

If you give us information about what the program should do, and what error message you get, that would be very helpful for us.
I hope you'll do this the next time.

Thank you.

Lets go to the program :
Here is a simple suggestion when we dont get the required output from a program:

We should test the functions one by one instead of using them all.
That would help in some cases to find out errors and debug the program.
Why dont you try this idea?

Since you have the variable number_used declared before the main function,
You need not pass them in the functions.
So you can remove the number_used in all of the functions.
like this:
CPP / C++ / C Code:
int get_array (int original_array[]);    //note that there is no number_used parameter here.
void display_array (int original_array []);
int reverse_array (int original_array []);
//do this for all the functions.

Then your main function should return int rather than being void.
So instead of void main() we can use int main()
Since the main is declared as int main, we should return 0 in the end of main.

The main error in the program is in this part:
CPP / C++ / C Code:
while(number_used>0)
{
cout<<"Enter element # "<<i +1<<": ";
cin>> original_array [i];
i++;
number_used --;
}

So you have used the number_used variable to display the numbers.
But the number_used finally reduces to zero inside the loop.
So after this function, the number_used will be zero and hence no other function would work!.
Lets think what should we do to solve this problem.
An Idea! We can use another variable say num instead of number_used.
And then use it in the loop.
Now, the number_used variable is unchanged!.
So we should modify the code as below:
CPP / C++ / C Code:
num = number_used;
while(num>0)
{
cout<<"Enter element # "<<i +1<<": ";
cin>> original_array [i];
i++;
num--;
}

And you cannot return two values using a return statement.
Infact, that return statement is not needed at all;(Why??)
We can remove it.

Once this problem is solved, the functions
display_array, find_max, find_min, sort_array will work

Now to the positive_negative function:
Lets see how to convert the original_array to positive and negative array?
Lets think of a simple idea.
Here it is:
Lets convert the original array into positive array first.
Then we can convert the positive array into negative array easily!!

Lets see how to do it:
Inside a for loop,
If the element of the original_array is positive, store it in positive_array without any change.
Else, store the negative of the original_array in the positive array.
Here is the thing:
CPP / C++ / C Code:
for( int i = 0; i < number_used ; i++ )
{
   if(original_array[ i ] >= 0)
   positive_array[ i ] = original_array[ i ];
   else
   positive_array[ i ] = - original_array[ i ];
}

Then we can calculate the negative array easily from the positive_array.
CPP / C++ / C Code:
for( int i = 0; i < number_used ; i++ )
{
   negative_array[ i ] = - positive_array[ i ]; 
}


We should remove the return statement here too..
Going fine till now.

The functions not working properly now are:
reverse_array, smallest_spread, largest_spread.

The reverse_array can be declared as void instead of int and we can print the values inside the function itself.
But why?
This is because we cannot return many values in a function.
Once we return any value, the function is terminated.
So the problem is that the return statement is inside the loop!!!

So we can modify the function to print the values. Here it is:
CPP / C++ / C Code:
void reverse_array (int original_array [])
{
  for( int i = number_used - 1 ; i >= 0; i-- )
  {
     cout<< original_array[ i ];      
  }
}

The only thing that remain now is largest and smallest spread.
What is the logical error involved in the program?
Ah. i got that.
We should use abs for calculating the sum and largest values.
We should not use fabs because it is for floats.
Since we use integers, we should use abs.
So if we correct the code as:
CPP / C++ / C Code:
smallest = abs(original_array [i] - original_array [j]);

sum = abs(original_array [i] - original_array [j]);

But there is still some error in this code.
Thinking.....
I got it.
The value of i and j has gone above the limit of the array.
the error is in the while statement.
so if we modify the while statement as this:
CPP / C++ / C Code:
while(number_used > j);

the error is gone.

Similarly we should change these for the largest spread also.

All the errors have gone now.

Cheers,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #4  
Old 26-Oct-2005, 23:09
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Need so much help!


Quote:
Originally Posted by Cat
Need help fixing the code to make this program output all the array! Help would be really nice!
I see a few things that are not helping.
In "get_array"
CPP / C++ / C Code:
int get_array (int original_array[], int number_used)
"number_used" is passed by value so any changes made to it will only change the value of the copy.

Even if you fix that so the original is there. You have this,
CPP / C++ / C Code:
	while(number_used>0)
	{
		cout<<"Enter element # "<<i +1<<": ";
		cin>> original_array [i];
		i++;
		number_used --;
	}
So when the function returns "number_used" will always be zero.
You also can only return one value not two.
CPP / C++ / C Code:
return (original_array [i] , number_used);
That also uses the "comma operator" and does not do what you might think it does.
"Comma Operator: ,"
http://msdn.microsoft.com/library/de...a_operator.asp

Also why return anything you just discard it anyway?
CPP / C++ / C Code:
get_array (original_array,number_used);

In "positive_negative" you have.
CPP / C++ / C Code:
int count;
You should give it a value before using it in a statement.
Then,
CPP / C++ / C Code:
while (count >=0)
you have made no provisions for that to ever fail.

I would suggest that you comment out all your functions but one. Get that function to work as intended then add the next one back fix that.... Umm you get the idea. You are trying to do to much at once.

As for the question.
Quote:
Originally Posted by Cat
Need help fixing the code to make this program output all the array!
That is a lot of code so you will need to ask a more specific question.
  #5  
Old 27-Oct-2005, 14:33
Cat Cat is offline
New Member
 
Join Date: Oct 2005
Posts: 11
Cat is on a distinguished road

Re: Array printing


Well now I am getting a error that says: cry:

C:\Documents and Settings\dboehm\My Documents\Lab9\Lab9.cpp(69) : error C2065: 'number_used' : undeclared identifier

It is for this part of the code and I do not know how to fix it

CPP / C++ / C Code:
void display_array (int original_array [])
{
	for(int i=0;i<number_used;i++)
	cout<< original_array[i];
	cout<<endl;

}

Does anyone know how I can fix it?
  #6  
Old 27-Oct-2005, 17:41
Cat Cat is offline
New Member
 
Join Date: Oct 2005
Posts: 11
Cat is on a distinguished road
Question

Re: Array printing


Well my program is running now. But my program is still not outputing the array. Does anyone know what could be worng

Here is what the display looks like:

CPP / C++ / C Code:
void display_array (int original_array [])
{
	for(int i=0;i<number_used;i++)
	cout<< original_array[i];
	cout<<endl;
}
  #7  
Old 27-Oct-2005, 17:44
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Array printing


Hi Cat,

Post your full code and maybe we can find what is the problem.

Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #8  
Old 27-Oct-2005, 17:51
Cat Cat is offline
New Member
 
Join Date: Oct 2005
Posts: 11
Cat is on a distinguished road

Re: Array printing


Here is all of the code!

[c++]
#include <iostream.h>
#include <math.h>
#define SIZE 20

void get_array (int original_array[]);
void display_array (int original_array []);
void reverse_array (int original_array []);
void find_max (int original_array []);
void find_min (int original_array []);
void sort_array (int original_array []);
void smallest_spread (int original_array []);
void largest_spread (int original_array []);
void positive_negative (int original_array [],int positive_array [], int negative_array []);
void swap_values (int& v1, int& v2);
int index_of_smallest (const int original_array [], int start_index);
int number_used;

int main()
{
int original_array [SIZE];
int positive_array [SIZE];
int negative_array [SIZE];
get_array (original_array);
cout<<"Original array: ";
display_array (original_array);
cout<<endl<<"Reversed array: ";
reverse_array (original_array);
cout<<endl<<"Maximum value = ";
find_max (original_array);
cout<<endl<<"Minimum value = ";
find_min (original_array);
cout<<endl<<"Sorted array: ";
sort_array (original_array);
display_array (original_array);
cout<<endl<<"Smallest spread = ";
smallest_spread (original_array);
cout<<endl<<"Largest spread = ";
largest_spread (original_array);
positive_negative (original_array,positive_array,negative_array);
cout<<endl<<" Positive array: ";
display_array (original_array);
cout<<endl<<"Negative array: ";
display_array (original_array);
cout<<endl;
return 0;
}
void get_array (int original_array[])
{
int number_used, num;
cout<<"Enter the array size: ";
cin>> number_used;
int i = 0;
num = number_used;
while(num>0)
{
cout<<"Enter element # "<<i +1<<": ";
cin>> original_array [i];
i++;
num--;
}
}
void display_array (int original_array [])
{
for(int i=0;i<number_used;i++)
cout<< original_array[i];
cout<<endl;

}
void reverse_array (int original_array [])
{
for( int i = number_used - 1 ; i >= 0; i-- )
{
cout<< original_array[ i ];
}
}

void find_max (int original_array [])
{
int max;
int i;
max = original_array [0];
for(i=1;i<number_used;i++)
{
if(original_array [i] > max)
max = original_array [i];
}
cout<<max;
}
void find_min (int original_array [])
{
int min;
int i;
min = original_array [0];
for(i=1;i<number_used;i++)
{
if(original_array [i] < min)
min = original_array[i];
}
cout<<min;
}
void sort_array (int original_array [])
{
int index_of_next_smallest;

for (int index = 0; index < number_used - 1; index ++)
{
index_of_next_smallest = index_of_smallest (original_array, index);
swap_values (original_array [index], original_array[index_of_next_smallest]);
}
}
void smallest_spread (int original_array [])
{
int i=0;
int j=1;
int smallest;
int sum;
smallest = abs(original_array [i] - original_array [j]);
do
{
sum = abs(original_array [i] - original_array [j]);

if(smallest > sum)
smallest = sum;
i++;
j++;
}
while(number_used > j);
cout<<abs (smallest);
}
void largest_spread (int original_array [])
{
int i=0;
int j=1;
int largest;
int sum;
largest = abs(original_array [i] - original_array [j]);
do
{
sum = abs(original_array [i] - original_array [j]);

if(largest < sum)
largest = sum;
i++;
j++;
}
while(number_used > j);
cout<< abs (largest);
}
void positive_negative (int original_array [],int positive_array [], int negative_array [])
{
for( int i = 0; i < number_used ; i++ )
{
if(original_array[ i ] >= 0)
positive_array[ i ] = original_array[ i ];
else
positive_array[ i ] = - original_array[ i ];
}
for( int j = 0; j < number_used ; j++ )
{
negative_array[ j ] = - positive_array[ j ];
}

}
void swap_values (int& v1, int& v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int index_of_smallest (const int original_array [], int start_index)
{
int min = original_array [start_index],
index_of_min = start_index;
for (int index = start_index + 1; index < number_used;
index ++)
if (original_array [index] < min)
{
min = original_array [index];
index_of_min = index;
}
return index_of_min;
}
[/ctt]
  #9  
Old 27-Oct-2005, 17:57
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Array printing


Hi Cat,

The error is in the function get_array();

The line where it is :
CPP / C++ / C Code:
int number_used, num;

You have created a duplicate value of number_used.
Remember the scope of the variables.

So, if we remove that number_used local variable, we'll solve the problem.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #10  
Old 27-Oct-2005, 18:02
Cat Cat is offline
New Member
 
Join Date: Oct 2005
Posts: 11
Cat is on a distinguished road

Re: Array printing


Hi Paramesh,
You are a life saver! Thank you so much! I got it to work
Thanks again!
Cat
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 10:25
Sorting Algorithms by Time silicon C++ Forum 4 02-May-2005 22:54
Help with an array using pointers glulu76 C++ Forum 1 01-May-2005 12:21
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:29.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.