473,796 Members | 2,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in making a program

An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list. Each file should
contain not more than 15 numbers.

Oct 12 '07 #1
9 1650
In article <11************ **********@v29g 2000prd.googleg roups.com>,
<ti************ **@gmail.comwro te:
>An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list. Each file should
contain not more than 15 numbers.
Which part of it are you having difficulty with? What code have you
come up with so far? Do you know how to read data from a file? Do you
know how to print out results? What is there in the specification that
calls for interaction with the user? What should happen to the sorted
list once it is created? Does the context of the assignment dictate
that a particular sorting algorithm should be used? What kind of
numbers are involved? If the files contain complex numbers, which of
the several competing decision algorithms will you use to decide
whether one of the complex numbers is "before" or "after" another? Is
it certain that there will not be garbage in the files? Will each file
contain the same number of numbers?
--
"History is a pile of debris" -- Laurie Anderson
Oct 12 '07 #2
<ti************ **@gmail.comwro te:
An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list. Each file should
contain not more than 15 numbers.
A single array with space for 45 elements could hold all the data and an
array is a nice starting point for a sort. Use your source code editor to
create the three sample files. I assume the interactivity consists of
getting the three files names from the user. If your instructor has talked
about pseudocode, it might be helpful to see what you can do with that, if
not, forget I mentioned it. You will have to pick up on clues the instructor
has given to know how to do the sort, it could be either a pre-written
routine or perhaps a simple sort you write yourself.
Oct 12 '07 #3
In article <5n************ @mid.individual .net>,
osmium <r1********@com ast.netwrote:
><ti*********** ***@gmail.comwr ote:
>An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list.
>A single array with space for 45 elements could hold all the data and an
array is a nice starting point for a sort.
A starting point, yes, but the requirement is to create "one sorted list",
not "one sorted array".

I would have thought that the instructions were a bit too simplistic
for a class that had reached linked lists, but "Ours is not to
reason why".
>Use your source code editor to
create the three sample files.
You are assuming that the input files are text. No such requirement
was stated: the files might be binary.
>I assume the interactivity consists of
getting the three files names from the user.
Hard to say. But anyone care to offer odds on the percentage of students
that don't flush the output buffer after prompting?
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)
Oct 12 '07 #4
"Walter Roberson" wrote:
In article <5n************ @mid.individual .net>,
osmium <r1********@com ast.netwrote:
>><ti********** ****@gmail.comw rote:
>>An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list.
>>A single array with space for 45 elements could hold all the data and an
array is a nice starting point for a sort.

A starting point, yes, but the requirement is to create "one sorted list",
not "one sorted array".

I would have thought that the instructions were a bit too simplistic
for a class that had reached linked lists, but "Ours is not to
reason why".
>>Use your source code editor to
create the three sample files.

You are assuming that the input files are text. No such requirement
was stated: the files might be binary.
>>I assume the interactivity consists of
getting the three files names from the user.

Hard to say. But anyone care to offer odds on the percentage of students
that don't flush the output buffer after prompting?
I was trying to help the student. I think you have some other goal in mind.
Oct 12 '07 #5
On Oct 12, 9:51 am, tiwarinitin.3.. .@gmail.com wrote:
An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list. Each file should
contain not more than 15 numbers.
Data:
You are going to need at least three file pointers.
You are going to need an array of at least 45 elements.

Steps:
You will send a prompt to stdout that asks for the name of the files.
You will read the three file names from standard input.
You will open the files.
You will read the numbers from the files and put them into the array.
You will then write a comparison function that compares two numbers
and returns -1 if the leftmost number is smaller, 0 if the numbers are
equal, and 1 if the rightmost number is larger. You will call qsort()
with the array and the comparison function as arguments.
You should print out the array to prove to yourself that it is sorted.

If you cannot perform the task given this outline together with your
textbook and what you have learned in class, then it will be
impossible for you to succeed in a computer science course.

It is time for you to give it an effort and show us what you have
done.

Oct 12 '07 #6
On Oct 12, 11:54 am, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <5n9t5dFh1ap... @mid.individual .net>,

osmium <r124c4u...@com ast.netwrote:
<tiwarinitin.3. ..@gmail.comwro te:
An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list.
A single array with space for 45 elements could hold all the data and an
array is a nice starting point for a sort.

A starting point, yes, but the requirement is to create "one sorted list",
not "one sorted array".

I would have thought that the instructions were a bit too simplistic
for a class that had reached linked lists, but "Ours is not to
reason why".
An array is a kind of list, but a list is not a kind of array.
IMO-YMMV.

{snip}

Oct 12 '07 #7
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <5n************ @mid.individual .net>,
osmium <r1********@com ast.netwrote:
>><ti********** ****@gmail.comw rote:
>>An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list.
>>A single array with space for 45 elements could hold all the data and an
array is a nice starting point for a sort.

A starting point, yes, but the requirement is to create "one sorted list",
not "one sorted array".

I would have thought that the instructions were a bit too simplistic
for a class that had reached linked lists, but "Ours is not to
reason why".
Hmm. It didn't occur to me that the word "list" in the description
means "linked list", particularly since it refers to lists being
stored in files. Your interpretation may be correct, but it's
impossible to tell.
>>Use your source code editor to
create the three sample files.

You are assuming that the input files are text. No such requirement
was stated: the files might be binary.
>>I assume the interactivity consists of
getting the three files names from the user.

Hard to say. But anyone care to offer odds on the percentage of students
that don't flush the output buffer after prompting?
Given the description, I have no idea what's supposed to be
interactive about the program. Given an identical description with
the word "interactiv e" deleted, it wouldn't occur to me to make the
program interactive; I'd probably get the input file names from the
command line arguments. I suppose a program that starts by printing
"Press any key to continue." and reading a character from stdin would
meet the stated requirements.

In any case, the stated requirements are extremely vague -- and we
can't tell whether it's the fault of the instructor, or if the OP
didn't quote them completely. And in the absence of any visible
effort by the OP, it's just a "DO MY HOMEWORK" posting.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 12 '07 #8
ti************* *@gmail.com wrote:
>
An interactive program that reads 3 list of numbers, which are stored
in three seperate files, and creates one sorted list. Each file should
contain not more than 15 numbers.
/* BEGIN new.c output */

FILE_1
1527239318
496027619
3472826252
3598182113
3148076786
1796534671
3248372296

FILE_2
1324866413
2920400654
309044219
3683030724
926430905
1233764074
2700036903

FILE_3
1369223424
8144069
4157315718
1601114899
587384060
4063891857
4244305378

Sorted Output File
8144069
309044219
496027619
587384060
926430905
1233764074
1324866413
1369223424
1527239318
1601114899
1796534671
2700036903
2920400654
3148076786
3248372296
3472826252
3598182113
3683030724
4063891857
4157315718
4244305378

/* END new.c output */
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#define FIFTEEN 7
#define NMEMB(A) (sizeof (A) / sizeof *(A))

#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069 + 362437 & 0XFFFFFFFFLU)

struct list_node {
struct list_node *next;
void *data;
};

typedef struct list_node list_type;

struct file_struct {
char *fn;
FILE *fp;
list_type *head, *tail;
};

list_type *list_sort(list _type *head,
int (*compar)(const list_type *, const list_type *));
void close_and_remov e(struct file_struct *file, size_t nmemb);
void list_free(list_ type *node, void (*free_data)(vo id *));
list_type *merge_lists(li st_type *head, list_type *tail,
int (*compar)(const list_type *, const list_type *));
int list_fputs(list _type *node, FILE *stream);
list_type *append_string( list_type **head,
list_type *tail,
char *string);
int get_line(char **lineptr, size_t *n, FILE *stream);
int numcomp(const list_type *a, const list_type *b);

static list_type *node_sort(list _type *head,
int (*compar)(const list_type *, const list_type *));
static list_type *list_split(lis t_type *head);
static list_type *list_merge(lis t_type *head, list_type *tail,
int (*compar)(const list_type *, const list_type *));

int main(void)
{
struct file_struct file[] = {
{"FILE_1"}, {"FILE_2"}, {"FILE_3"}
};
size_t index, number;
long unsigned lu_seed;
int rc;
char *buff;
size_t size;
list_type *output;
FILE *fp;
char *fn = "out";

puts("/* BEGIN new.c output */\n");
output = NULL;
buff = NULL;
size = 0;
lu_seed = LU_RAND_SEED;
for (index = 0; index != NMEMB(file); ++index) {
file[index].fp = fopen(file[index].fn, "w");
if (file[index].fp == NULL) {
close_and_remov e(file, index);
puts("fopen(fil e[index].fn, \"w\") == NULL");
exit(EXIT_FAILU RE);
}
for (number = 0; number != FIFTEEN; ++number) {
lu_seed = LU_RAND(lu_seed );
fprintf(file[index].fp, "%lu\n", lu_seed);
}
fclose(file[index].fp);
}
for (index = 0; index != NMEMB(file); ++index) {
file[index].fp = fopen(file[index].fn, "r");
if (file[index].fp == NULL) {
close_and_remov e(file, index);
printf("fopen(% s, \"r\") == NULL", file[index].fn);
exit(EXIT_FAILU RE);
}
while ((rc = get_line(&buff, &size, file[index].fp)) 0) {
file[index].tail = append_string
(&(file[index].head), file[index].tail, buff);
if (file[index].tail == NULL) {
puts("file[index].tail == NULL");
break;
}
}
puts(file[index].fn);
list_fputs(file[index].head, stdout);
putchar('\n');
file[index].head = list_sort(file[index].head, numcomp);
output = merge_lists(out put, file[index].head, numcomp);
}
free(buff);
close_and_remov e(file, NMEMB(file));
puts("Sorted Output File");
list_fputs(outp ut, stdout);
putchar('\n');
fp = fopen(fn, "w");
if (fp == NULL) {
printf("NULL = fopen(%s, \"w\")", fn);
}
list_fputs(outp ut, fp);
list_free(outpu t, free);
fclose(fp);
remove(fn);
puts("/* END new.c output */");
return 0;
}

void close_and_remov e(struct file_struct *file, size_t nmemb)
{
while (nmemb-- != 0) {
fclose(file[nmemb].fp);
remove(file[nmemb].fn);
}
}

list_type *list_sort(list _type *head,
int (*compar)(const list_type *, const list_type *))
{
return head != NULL ? node_sort(head, compar) : head;
}

void list_free(list_ type *node, void (*free_data)(vo id *))
{
list_type *next_node;

while (node != NULL) {
next_node = node -next;
free_data(node -data);
free(node);
node = next_node;
}
}

list_type *merge_lists(li st_type *head, list_type *tail,
int (*compar)(const list_type *, const list_type *))
{
if (tail != NULL) {
if (head != NULL) {
head = list_merge(head , tail, compar);
} else {
head = tail;
}
}
return head;
}

int list_fputs(list _type *node, FILE *stream)
{
while (node != NULL) {
if (fputs(node -data, stream) == EOF) {
return EOF;
}
if (putc('\n', stream) == EOF) {
return EOF;
}
node = node -next;
}
return '\n';
}

list_type *append_string( list_type **head,
list_type *tail,
char *string)
{
list_type *node;

node = malloc(sizeof *node);
if (node != NULL) {
node -next = NULL;
node -data = malloc(strlen(s tring) + 1);
if (node -data != NULL) {
strcpy(node -data, string);
if (*head != NULL) {
tail -next = node;
} else {
*head = node;
}
} else {
free(node);
node = NULL;
}
}
return node;
}

int get_line(char **lineptr, size_t *n, FILE *stream)
{
int rc;
void *p;
size_t count;

count = 0;
while ((rc = getc(stream)) != EOF) {
if (count != (size_t)-2) {
++count;
}
if (count + 2 *n) {
p = realloc(*linept r, count + 2);
if (p == NULL) {
if (*n count) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
} else {
if (*n != 0) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (rc == '\n') {
(*lineptr)[count - 1] = '\0';
break;
}
(*lineptr)[count - 1] = (char)rc;
}
if (rc != EOF) {
rc = INT_MAX count ? count : INT_MAX;
} else {
if (*n count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}

int numcomp(const list_type *a, const list_type *b)
{
const long unsigned a_num = strtoul(a -data, NULL, 10);
const long unsigned b_num = strtoul(b -data, NULL, 10);

return b_num a_num ? -1 : b_num != a_num;
}

static list_type *node_sort(list _type *head,
int (*compar)(const list_type *, const list_type *))
{
list_type *tail;

if (head -next != NULL) {
tail = list_split(head );
tail = node_sort(tail, compar);
head = node_sort(head, compar);
head = list_merge(head , tail, compar);
}
return head;
}

static list_type *list_split(lis t_type *head)
{
list_type *tail;

tail = head -next;
while ((tail = tail -next) != NULL
&& (tail = tail -next) != NULL)
{
head = head -next;
}
tail = head -next;
head -next = NULL;
return tail;
}

static list_type *list_merge(lis t_type *head, list_type *tail,
int (*compar)(const list_type *, const list_type *))
{
list_type *list, *sorted, **node;

node = compar(head, tail) 0 ? &tail : &head;
list = sorted = *node;
*node = sorted -next;
while (*node != NULL) {
node = compar(head, tail) 0 ? &tail : &head;
sorted -next = *node;
sorted = *node;
*node = sorted -next;
}
sorted -next = tail != NULL ? tail : head;
return list;
}

/* END new.c */
--
pete
Oct 16 '07 #9
pete wrote:
fp = fopen(fn, "w");
if (fp == NULL) {
printf("NULL = fopen(%s, \"w\")", fn);
}
list_fputs(outp ut, fp);
list_free(outpu t, free);
fclose(fp);

fp = fopen(fn, "w");
if (fp == NULL) {
printf("NULL = fopen(%s, \"w\")", fn);
} else {
list_fputs(outp ut, fp);
fclose(fp);
}
list_free(outpu t, free);

--
pete
Oct 16 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
13883
by: Trevor Fairchild | last post by:
I am making a program that categorizes pictures. The picture paths are stored in an Access Database, vb6 connects using adodc controls. This program works specifically with .jpg files. It will be used by people categorizing thousands of images. The problem I have is that both the picture control, and the image control seem unable to open certain jpg images for an unknown reason. The error message simply states "Invalid Picture". I can...
6
3311
by: Iain Bishop | last post by:
I'm trying to model objects for the following problem: A building site contains assemblies, each of which can contain other assemblies and/or materials. I have modelled this using a Site class, Assembly class, and Material class as follows... Site Class (clsSite): Option Explicit
117
7273
by: Peter Olcott | last post by:
www.halting-problem.com
1
1336
by: ashutosh | last post by:
hi, i am making the dll for clamav antivirus libraray so that i can make activex control for windows. i complied the library successfully and make the Libclamav.dll file using win32 Api project in debug mode. this dll file exports all the 21 function which is needed for clamscan.
2
1727
by: clr | last post by:
I'm Using Windows XP and Visual Basic .Net 2002. When I press F5 <Debug/Start>, repeatedly, the program does not always function. However if I consistently press F11 <Step into> and then press F5 the program executes every time. I can reproduce this problem with a simple program that only displays a "Hello World" startup form Steps to Reproduce <Press F5> <Close Application> <Press F5> The Output Window shows that the program...
2
1792
by: Daya Kiran Sunkara | last post by:
Hi All, I have a program which fetches the list of files inside a directory. For fetching this list I am making use of the glob.glob method which takes path as a parameter. For building the path I am making use of os.path.join. My code looks somewhat like this: ----------------------------------------------------------------------- import glob import os
10
1913
by: strife | last post by:
Hey everyone, I was making a program for a class, and I ran into a problem that is driving me crazy. I first suspected Vista, and since I am not home I cannot verify if it just my Vista computer, anyway, here is the problem. I know this is a lame program, but I lacked the creativity to come up with a program including all the required items in three days. I ended up getting 351/585(60%) on this because, I didn't include 2 arrays,...
2
4407
by: Praesidium | last post by:
I'm having a bit of a problem running a C program I'm working on (compiled in cygwin). There's a way to go, as I'm intent on making sure each bit works before moving on to the next. As it appears now: # include <stdio.h> # include <string.h> int main(void) { printf("Fine at line 6"); FILE *source; char string1;
0
1976
by: Filemaxor | last post by:
I have gotten my code to be able to allow people to add new text to a .txt document and able to call up files so the user can see whats in it. The problem i'm having is getting the for loop to work correctly so that i can allow certain indexes to be removed without completely deleting everything else. This is what i have so far. Code is below It's in the e part of the code and I have //remed the location of the for loop containg the...
6
3642
by: James Dow Allen | last post by:
On May 7, 11:24 pm, sophia <sophia.ag...@gmail.comwrote: Contrary to a suggestion in this thread, the straightforward solution to this puzzle is not of exponential complexity, but rather N^3/3. Yes, I know O(N^3) = O(N^3/3) but I show the constant divisor to emphasize that the iterative structure is related to the volume of a pyramid. I'm enclosing my source code (and cross-posting to
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.