#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<ostream>
staticconstint size = 100;
staticconstint row = 5;
int swapholder = -1;
int length = 100;
int end = 100;
usingnamespace std;
int main ()
{
int array1[size]; // array of 100 rows and 5 columns
fstream textfile;
textfile.open("arrayfill.txt"); // my text file
textfile << "writing this to a file.\n";
textfile.close();
for (int i =0; i< size; i++){
array1[i]= rand()% 100; // for random values 0-99
}
for (int counter = length -1; counter > 0; counter--){
for (int index =0; index < 10; index++){
if (array1[index] > array1[index+1]){
swapholder = array1[index+1];
array1[index+1] = array1[index];
array1[index] = swapholder;
}
endl--;
}
for (int index =0; index < 100; index++){
cout << array1[index] << ",";
}
cout << endl;
}
int k = 1;
for (int i =0; i < size; i++, k++){
cout << array1[i] << " ";
if (k == row ) {
cout << endl;
k =0;
}
}
return 0;
}
I meant to put end-- but the reason i put it is stop after every pass through the loop reduce the number so that it can put them in order. This is the new error-
program3.cpp:51:1: error: reference to 'end' is ambiguous
end--;
^
program3.cpp:16:5: note: candidate found by name lookup is 'end'
int end = 100;
^
This is exactly why one shouldnt use usingnamespace std; but rather explicity state for example std::cout There is a function in the std namespace called end, so when you have usingnamespace std, c++ doesn't know whether you are using the std::end or your integer end.
You should probably stop using the namespace, if you for some reason insist on using it, then change the variable name.
Also you shouldnt be using global variables, they're bad news. You can read about both these things on the googles.
Google why usingnamespace std is bad and why not to use global variables.
First of all, static const is kind of redundant in C++. Global variables are static by default, so you could've just done
1 2
constint size = 100;
constint row = 5;
Second, like TarikNeaj said, global variables aren't good practice. Constants are OK to be global but not the variables. Try to keep variables local and instead, use the appropriate parameters.
Third, my compiler also complained about end --; because of "using namespace std;". Basically, the compiler is confused whether I meant the variable end (which was created by you) or std::end. I am an university student as well and YES, my first intro programming class taught me to use using namespace std;. I've learned to get away from it though. It's OK to use "using ..." if you know when to use and when not to use it. But if you just make a habit of avoiding it, that works too.
Fourth, if you are using rand (), include <cstdlib> library. Somehow, it looks like you managed to compile your code without that library. But be aware that rand () function is from <cstdlib> and some compilers will complain if you don't include that library. If you are going to use rand (), be sure to provide seed so that way the number will be random EVERY time you run the program <http://www.cplusplus.com/reference/cstdlib/rand/?kw=rand>
1 2 3 4 5 6 7 8 9 10 11
#include <cstdlib>
#include <ctime>
int main ()
{
int iSecret;
srand (time(NULL)); // Initialize pseudo random number generator
iSecret = rand() % 10 + 1; // Then, call rand ()
}
DEBUGGING:
Lastly, I'd like to help you debug it but there is very little information as to how the program should behave. You gave us the code and you told us the error messages. But, you haven't told us how the program should run. For example, what's the expected output result when you run the program. What is the purpose of the program. What are the specifications?
I will still try to help you with this. But, I will be making some assumptions.
I can tell that at the beginning of the program, you opened a text file and saved a sentence in the file and then immediately closed the file. I created the text file on my computer and made sure that this portion of the code works. Then, you saved random numbers in array1. After line 32, i commented out the rest of the code and included the following debugging line.
1 2
for ( int iter = 0 ; iter < size ; iter ++ )
std::cerr << array1 [iter] << std::endl;
As expected, a random number was stored in each element of array1. Those numbers were exactly same each time I ran the program and that's because you did not provide seed for the engine.
I un-commented the next portion of your code, which is Line 32 ~ Line 48. counter variable is initialized to 99 in the for-loop and it runs as long as counter > 0. counter is decremented after each iteration. swapholder is used to temporarily hold a value for swapping purpose. So, is there any particular reason why you initialized swapholder to -1? And why couldn't swapholder be inside the main function? Looks like the purpose of this for-loop is to sort the first 10 elements of array1 from smallest to largest. Am I correct? You also have another for-loop for printing out the result, but that for-loop for printing is INSIDE the for-loop for sorting. Each time the sort for-loop runs, you print the elements. I guess this is your first mistake. Take the print for-loop out of the sort for-loop. Print the sorted elements after sorting is finished. Again, because of Line 36, you are sorting the first 10 elements of the array1.
I un-commented the last portion of your code, which is Line 60 ~ Line 72. The purpose of this portion is to display 5 elements in each row. It seems to work as intended.
EDIT: Like TarikNeaj said, you better learn how to debug your code. But, I've tried to help since you said you spent days trying to fix it. I've been through that experience myself when I was working on school assignments and I felt the same pain and desperation.
This is the final code after I debugged it based on my own assumptions. See if this is what YOU expected. If it is not, you tell us what the program is supposed to do and we will do our best to help.
Program Specifications:
1. Create an array to store 100 elements
2. Store a random number in each slot in the array
3. Sort the first 10 elements from smallest to largest
4. Print all the elements after sorting
5. Print all elements with 5 elements in each row (20 Rows Total).
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <ostream>
#include <cstdlib>
#include <ctime>
constint size = 100;
constint row = 5;
usingnamespace std;
int main ()
{
int swapholder; // This will be used for swapping two variables. No need to initialize this really
int length = 100;
int array1[size]; // array of 100 rows and 5 columns
fstream textfile;
textfile.open("arrayfill.txt"); // my text file
textfile << "writing this to a file.\n";
textfile.close();
srand ( time( 0 ) ); // time (0) used as seed
// Store a random number in each element of the array
for ( int i = 0 ; i < size ; i ++ ){
array1[i] = rand () % 100; // for random values 0-99
}
// Sort the first 10 elements of the array from smallest to largest
for ( int iter = 0 ; iter < 10 ; iter ++ ) {
for (int index = iter + 1 ; index < 10 ; index ++ ){
if ( array1 [iter] > array1 [index] ){
swapholder = array1 [iter];
array1 [iter] = array1 [index];
array1 [index] = swapholder;
}
}
}
// Print each element in the array
for (int index =0; index < 100; index++){
cout << array1[index] << " ";
}
cout << endl << endl;
int k = 1;
// Print each element, 5 in each row.
for (int i = 0 ; i < size ; i ++, k ++ ){
cout << array1 [i] << " ";
if ( k == row ) {
cout << endl;
k = 0;
}
}
return 0;
}