![]() |
|
#1
|
|||
|
|||
Array printingNeed help fixing the code to make this program output all the array! Help would be really nice!
CPP / C++ / C Code:
Last edited by LuciWiz : 27-Oct-2005 at 03:23.
Reason: Please insert your C code between [c] & [/c] tags
|
|||
|
#2
|
|||
|
|||
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
|
||||
|
||||
Re: Array problemHi 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:
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:
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:
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:
Then we can calculate the negative array easily from the positive_array. CPP / C++ / C Code:
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:
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:
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:
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
|
|||
|
|||
Re: Need so much help!Quote:
In "get_array" CPP / C++ / C Code:
Even if you fix that so the original is there. You have this, CPP / C++ / C Code:
You also can only return one value not two. CPP / C++ / C Code:
"Comma Operator: ," http://msdn.microsoft.com/library/de...a_operator.asp Also why return anything you just discard it anyway? CPP / C++ / C Code:
In "positive_negative" you have. CPP / C++ / C Code:
Then, CPP / C++ / C Code:
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:
|
|
#5
|
|||
|
|||
Re: Array printingWell 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:
Does anyone know how I can fix it? |
|
#6
|
|||
|
|||
Re: Array printingWell 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:
|
|
#7
|
||||
|
||||
Re: Array printingHi 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
|
|||
|
|||
Re: Array printingHere 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
|
||||
|
||||
Re: Array printingHi Cat,
The error is in the function get_array(); The line where it is : CPP / C++ / C Code:
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
|
|||
|
|||
Re: Array printingHi Paramesh,
You are a life saver! Thank you so much! I got it to work Thanks again! Cat |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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