Hi there,
i have a problem. I prepared bucket sorting with reading values from
file (data.txt) and saving to other file (aus.txt).
Everything is good if we talk about reading and sorting, but saving
doesn't work.
data.txt looks:
4
3
67
15
1
4
13
87
After compiling and running file aus txt lokks like this:
4011840
Where is the failure? Could anyone give me corrected source?
This is my code:
#include <iostream>
#include <conio.h>
#include <string.h>
#include <fstream.h>
//#include <stdio.h>
//using namespace std;
int readData(int numbers[10])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers[i])) {
i++;
}
return i;
}
void saveToFile(int table[10],char *name)
{
ofstream pl2;
pl2.open(name);
pl2 << table[10];
pl2.close();
}
int MAX=100;
int bucket[10];
int bucketsort(int numbers[10], int n)
{
int i, m;
i=0;
while(i<MAX) {
bucket[i] = 0;
i=i+1;
}
i=0;
while(i<n) {
bucket[numbers[i]] = bucket[numbers[i]] + 1;
i=i+1;
}
m=0;
i=0;
while(i<MAX) {
while(bucket[i]>0) {
numbers[m] = i;
m=m+1;
bucket[i] = bucket[i]-1;
}
i=i+1;
}
return 0;}
int main(int argc, int* argv[])
{
char file_name;
char *file="aus.txt";
int i,il;
int table[10];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
bucketsort(table, il);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
saveToFile(table,file);
return 0;
}
--------------
Thanks in advance for your support!
Regards,
Luke 5 1732
^cypis^ vel. SQ9JTI <sq****@gmail.comwrote:
Hi there,
i have a problem. I prepared bucket sorting with reading values from
file (data.txt) and saving to other file (aus.txt).
Everything is good if we talk about reading and sorting, but saving
doesn't work.
data.txt looks:
4
3
67
15
1
4
13
87
After compiling and running file aus txt lokks like this:
4011840
Where is the failure? Could anyone give me corrected source?
This is my code:
#include <iostream>
#include <conio.h>
Non-standard header
#include <string.h>
#include <fstream.h>
Non-standard header, use <fstream(then you need to qualify ofstream as
std::ofstream, or use "using std::ofstream;" or "using namespace std;").
//#include <stdio.h>
//using namespace std;
Also, I find it a little non-idiomatic to mix C-style FILE*'s with
C++-style fstreams.
int readData(int numbers[10])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers[i])) {
i++;
}
return i;
}
void saveToFile(int table[10],char *name)
{
ofstream pl2;
pl2.open(name);
pl2 << table[10];
pl2.close();
}
There is not a suitable default operator<< for arrays that does what you
want. You need to loop through the values in the array and output them
individually (similarly to how you print them to the screen in main()).
Also, you have Undefined Behavior because table is an array with 10
elements (indexed from 0 to 9), so table[10] does not exist. Even if
you corrected it to be table[9], it wouldn't output the entire array to
the file, but only the last element.
int MAX=100;
int bucket[10];
int bucketsort(int numbers[10], int n)
{
int i, m;
i=0;
while(i<MAX) {
bucket[i] = 0;
i=i+1;
}
bucket only has 10 elements as declared, but in this loop you loop 100
times. This is Not Good.
i=0;
while(i<n) {
bucket[numbers[i]] = bucket[numbers[i]] + 1;
i=i+1;
}
m=0;
i=0;
while(i<MAX) {
while(bucket[i]>0) {
numbers[m] = i;
m=m+1;
bucket[i] = bucket[i]-1;
}
i=i+1;
}
return 0;}
int main(int argc, int* argv[])
Should be:
int main(int argc, char* argv[]) // argv is array of char*, not int*
{
char file_name;
Doesn't appear to be used, and is also misleading since a file name
should be longer than 1 character.
char *file="aus.txt";
file should be const char* instead of just char*.
int i,il;
int table[10];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
bucketsort(table, il);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
saveToFile(table,file);
return 0;
}
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
^cypis^ vel. SQ9JTI wrote:
Hi there,
i have a problem. I prepared bucket sorting with reading values from
file (data.txt) and saving to other file (aus.txt).
Everything is good if we talk about reading and sorting, but saving
doesn't work.
data.txt looks:
4
3
67
15
1
4
13
87
After compiling and running file aus txt lokks like this:
4011840
Where is the failure? Could anyone give me corrected source?
This is my code:
Your program contains lots of loops. This is good. Now add one more loop
and save the elements of your array ONE AT A TIME. You cannot save a
array all at once, you have to write a loop and save each element
seperately.
john
Hi,
it's workung, this is the code:
------------------------------------------
#include <iostream>
#include <conio.h>
#include <string.h>
#include <fstream.h>
//#include <stdio.h>
//using namespace std;
int readData(int numbers[9])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers[i])) {
i++;
}
return i;
}
void saveToFile(int table[9],char *name)
{
ofstream pl2;
pl2.open(name);
for (int i=0;i<9;i++)
{
pl2 << table[i];
pl2 << "/n ";
}
pl2.close();
}
int MAX=100;
int bucket[9];
int bucketsort(int numbers[9], int n)
{
int i, m;
i=0;
while(i<MAX) {
bucket[i] = 0;
i=i+1;
}
i=0;
while(i<n) {
bucket[numbers[i]] = bucket[numbers[i]] + 1;
i=i+1;
}
m=0;
i=0;
while(i<MAX) {
while(bucket[i]>0) {
numbers[m] = i;
m=m+1;
bucket[i] = bucket[i]-1;
}
i=i+1;
}
return 0;}
int main(int argc, int* argv[])
{
char file_name;
char *file="aus.txt";
int i,il;
int table[9];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
bucketsort(table, il);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
saveToFile(table,file);
return 0;
}
-----
It is the newest version. but now i receive srted file with bonus
value: aus.txt. looks like this:
1 3 4 4 13 15 67 87 2009249812
What for is this 2009249812 ?
Thanks for your answers!
Regards,
Luke
^cypis^ vel. SQ9JTI wrote:
Hi,
it's workung, this is the code:
------------------------------------------
#include <iostream>
#include <conio.h>
#include <string.h>
#include <fstream.h>
//#include <stdio.h>
//using namespace std;
int readData(int numbers[9])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers[i])) {
i++;
}
return i;
}
void saveToFile(int table[9],char *name)
{
ofstream pl2;
pl2.open(name);
for (int i=0;i<9;i++)
{
pl2 << table[i];
pl2 << "/n ";
}
pl2.close();
}
int MAX=100;
int bucket[9];
int bucketsort(int numbers[9], int n)
{
int i, m;
i=0;
while(i<MAX) {
bucket[i] = 0;
i=i+1;
}
i=0;
while(i<n) {
bucket[numbers[i]] = bucket[numbers[i]] + 1;
i=i+1;
}
m=0;
i=0;
while(i<MAX) {
while(bucket[i]>0) {
numbers[m] = i;
m=m+1;
bucket[i] = bucket[i]-1;
}
i=i+1;
}
return 0;}
int main(int argc, int* argv[])
{
char file_name;
char *file="aus.txt";
int i,il;
int table[9];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
bucketsort(table, il);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
saveToFile(table,file);
return 0;
}
-----
It is the newest version. but now i receive srted file with bonus
value: aus.txt. looks like this:
1 3 4 4 13 15 67 87 2009249812
What for is this 2009249812 ?
Thanks for your answers!
Regards,
Luke
If you look at the original file there are EIGHT numbers. If you look at
your code you read NINE numbers. That is where the extra number comes from.
Another issue with you code.
int MAX=100;
int bucket[9];
int bucketsort(int numbers[9], int n)
{
int i, m;
i=0;
while(i<MAX) {
bucket[i] = 0;
i=i+1;
}
bucket has a size of 9, MAX is 100, therefore this loop is writing to
elements of the array that don't exist. You are lucky that your program
doesn't crash. Perhaps you meant this
int MAX=100;
int bucket[MAX];
John
^cypis^ vel. SQ9JTI wrote:
Hi,
it's workung, this is the code:
------------------------------------------
#include <iostream>
#include <conio.h>
This is a non-standard header and it's contents are unused.
#include <string.h>
This include brings in the strXXX functions. You do not use them.
In addition, you should use <cstringinstead, if you wish to use strXXX.
If you are attempting to use a std::string, then you should
#include <string>
#include <fstream.h>
This is a non-standard header and should not be included.
Use #include <fstream>
//#include <stdio.h>
This is needed for fopen, fscanf.
//using namespace std;
You *need* either this, or you need to qualify ofstream with std::.
>
int readData(int numbers[9])
{
FILE *pl;
int i = 0;
pl = fopen("data.txt", "r");
if (pl == NULL)
printf("file not found");
while (1==fscanf(pl, "%d", &numbers[i])) {
i++;
}
return i;
Resource leak here, you never fclose p1.
}
void saveToFile(int table[9],char *name)
{
ofstream pl2;
pl2.open(name);
for (int i=0;i<9;i++)
{
pl2 << table[i];
pl2 << "/n ";
}
pl2.close();
}
int MAX=100;
int bucket[9];
int bucketsort(int numbers[9], int n)
{
int i, m;
i=0;
while(i<MAX) {
bucket[i] = 0;
i=i+1;
}
i=0;
while(i<n) {
bucket[numbers[i]] = bucket[numbers[i]] + 1;
i=i+1;
}
m=0;
i=0;
while(i<MAX) {
while(bucket[i]>0) {
numbers[m] = i;
m=m+1;
bucket[i] = bucket[i]-1;
}
i=i+1;
}
return 0;}
int main(int argc, int* argv[])
{
char file_name;
char *file="aus.txt";
int i,il;
int table[9];
ile = readData(table);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
bucketsort(table, il);
for (i = 0; i < il; i++)
printf("%d ", table[i]);
printf("\n");
saveToFile(table,file);
return 0;
}
-----
It is the newest version. but now i receive srted file with bonus
value: aus.txt. looks like this:
1 3 4 4 13 15 67 87 2009249812
What for is this 2009249812 ?
Thanks for your answers!
Regards,
Luke This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dont bother |
last post by:
This is really driving me crazy.
I have a dictionary feature_vectors{}. I try to sort
its keys using
#apply sorting on feature_vectors
sorted_feature_vector=feature_vectors.keys()...
|
by: Tedy |
last post by:
Hi!
I have XML document that looks in shortcut like this:
< category >
< block >
|
by: kan |
last post by:
I am sorry,I have poor english..
Two Class file is located below one namespace..
it is no error..
namespace Convert (namespace name)
WinForm (first class name)
ColumnSorter (second class...
|
by: Julie |
last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB)
for a given string.
The files are unindexed and unsorted, and for the purposes of my immediate
requirements, can't...
|
by: zjut |
last post by:
I want to add a string to the file and the file is sort by letter! for
examply:
the follow file is a big file
//////////////////////
abort
black
cabbage
dog
egg
fly
|
by: hoopsho |
last post by:
Hi Everyone,
I am trying to write a program that does a few things very fast
and with efficient use of memory...
a) I need to parse a space-delimited file that is really large,
upwards fo a...
|
by: Cerebrus99 |
last post by:
Hi all,
I am confused about how to sort an XML file. I mean how to *actually*
sort the data in the physical file, not how to display sorted data. I
am using a large XML file as a back-end...
|
by: Claudio Grondi |
last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want
to change only eight bytes in this file at a given offset of appr. 200
Gbyte (all other data in that file should remain...
|
by: bisuvious |
last post by:
hi all,
I am looking for a technique to sort data of a large file, say 2GB/
4GB.
Please suggest any algorithm, if you have any idea.
thanks
bisuvious
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |