Solved

Need help with construction & debug.

Posted on 2001-06-04
9
297 Views
Last Modified: 2010-04-02
These are not homework. Just trying to execute in C for the purpose
of using on my Web site. I'm a Java/Perl/Server programmer. My problem
right now is getting a grip on C syntax which is bit more time consuming
than I anticipated.
 
Not sure if you offer any help with this, but if you have any friends who know any
thing about C structuring may be can give some advice on how to proceed.
 
These mini programs are part of a larger execution once they're done. Then they
can be consolidated into a larger application package.
 
By the feel free to use any code free whether any advice is given or not. There is neat
little Movie app program. I used this concept to construct a Java & Perl program to
do the same thing.
 
Here are the mini programs:
 
" Fred Mayes
Frederick.mayes@webcompinc.com

The Programs below are just a few that I am having trouble constructing. Any help is greatly appreciated!!
#1

This is a program that should output a bank statement. I must enter a beginning BALANCE and TRANSACTION at the same time. For example, if the transact is a deposit must be preceded by ?D'; withdrawal is ?W'; and checks ?C' with check number. Checks need not be entered in numerical order.
Should output a full report sorted by check number.
NOTE: You can use whatever functions you're comfortable with such as Structure Arrays.
------------------------------------------------------------------------
INPUTDATA: Enter
Begn Bal: 1427.89

Trans:
(W)ithdraw, (D)eposit, (C)eck, (Q)uit: D

Amt: 2478.36
?? ?? ?? ?? C

Chk Number: 1035
Amt: 95.23
?? ?? ?? ?? W
Amt: 160
?? ?? ?? ?? C
Chk Number: 1032
Amt: 46.41
?? ?? ?? ?? W
Amt: 45.50
?? ?? ?? ?? C
Chk Number: 1033
Amt: 25.72
?? ?? ?? ?? Q
Thank You!
----------------------------------------------------------------------
XYZ Header file: Mergsort.h
Contain functions(methods) declarations

#define MAX_ARRAY 512 /*Const Declaration*/

void merge_sort( int[], int, int ); /*Function Declaration*/
void merge( int[], int, int,int,int);
void move(int[], int, int, int[], int);

-----------------------------------------------------------------------
XYZ Bank Program: Mergsort.c

#include "mergsort.h"
#include <stdio.h>
#include <ctype.h>

#define DONE 0
#define MORE 1
#define ERROR -1
#define MAX_ARRAY 512

int print_array( int[], int, int );
int fill_array( int[] );
int get_int( int * );

void merge_sort( int[],int,int);
void merge( int[],int,int,int,int);
void move(int[],int,int,int[],int);

int main( void )
{

int array[MAX_ARRAY];
int num_items;


num_items = fill_array( array);
merge_sort( array, 0, num_items-1 );
print_array ( array, 0, num_items-1 );
}
int fill_array( int empty[] )
{

int count = -1;
int more;
more = MORE;

printf( "Enter your integers now. Enter 'Q' to quit\n" );
printf( "%3d : ", ++count );

while ( count < MAX_ARRAY )
{
more = get_int ( empty + count );

if ( more == ERROR )
{
printf( "Error in input - Try again\n" );
printf( "%3d : ", count+1 );
continue;
}
if ( !more )
break;
printf( "%3d : ", ++count+1 );
}
return ( count );
}
int print_array( int array[], int first, int num_items )
{

int index;
for ( index = first; index <= num_items; index++ )
print( "%3d:%5d\n", index+1, array[index] );
}
int get_int( int *intptr )
{

int ch;
char remainder[80];
int digit_count = 0;

*intptr = 0;
while ( isdigit(ch = getchar() ))
{

*intptr = *intptr * 10 + (ch - '0' );
digit_count++;
}
switch ( ch ){
case 'Q':
case 'q':
gets( remainder );
return ( DONE );
case ' ':
gets( remainder );
case '\n':
if ( digit_count )
return ( MORE );
else
return ( ERROR );
default:
gets( remainder );
return ( ERROR );
}
}
void merge_sort( int to_sort, int first, int last )
{

if ( first < last )
{
merge_sort( to_sort, first, (first+last)/2 );
{
merge_sort( to_sort, first, (first+last)/2 );

merge_sort( to_sort, (first+last)/2+1, last );

merge ( to_sort, first, (first+last)/2,
(first+last)/2+1, last );
}
}
void merge( int lists[], int first3, int last1,
int first4, int last2 )
{

int temp[MAX_ARRAY];
int index, index1, index2;
int first1,first2;
int num_items;

index = 0;
index1 = first1;
index2 = first2;

num_items = last1 - first1 + last2 - first2 +2;

while (( index1 <= last1 ) && ( index2 <= last2 ))
{
if ( lists[index1] < lists[index2] )
temp[index++] = lists[index1++];
else
temp[index++] = lists[index2++];
}
if ( index1 > last1 )
move( lists, index2, last2, temp, index );
else
move( lists, index1, last1, temp, index );

move( temp, 0, num_items-1, lists, first1 );
}
void move( int list1[], int first1, int last1,
int list2[], int first2 )
{
while ( first1 <= last1 )
list2[ first2++ ] = list1[ first1++ ];
}


#2
Recursive program:

I must write a recursive function that calculates the n-th power of a floating point value x. Both n and x should be parameters to the function. The return value of the function will the value of x^n.



#3
Structure Functions:
2.
a. I must write a structure (struct card) that will represent al full deck of playing cards (struct card deck[52], both the suits(hearts spades, etc) and ranks(A,K,Q,J,10,9,8,etc).

b. Write next function to shuffle the deck, broken in half & rearranged so that the 1st card is succeeded by the 27th, second followed by the 28th, and so on.


c. Write a program to test how many shuffles will return deck to original position.
8.
a. Write a struct declaration ( struct string ) . The fields should be named ?chars? and ?length?.
b. A strlen() version called mystrlen().
c. A strcat() version called mystrsrcat()
d. ?print_string? function that takes a ?struct string? as a parameter & outputs the characters to the screen.
e. Put your code for parts a,b,c, and d in a file named ?mystring.h?, and make sure the code works with:

#include <stdio.h>
#include?mystring.h?

void main( void )
{
struct string str1,
str2 = {?how now brown cow?, 17};

str1.length = 0

printf ( ?Lenth of str1 ?%d\n?, mystrlen( str1));

mystrcat( str1, str2 );
print_string( str1);
printf( ?\nLength of str1 - %d\n?, mystrlen( str1 ));
}

---------------------------------------------------------------------------------------------------------------------------------
#4

Write a program that accepts input of the data in the form of 1/14/94 and outputs it in the form January 14, 1994. Plan the communication between parts of programs before you start coding.


#5

Multiarrays & Dle Indirection:

a. Write a function that takes 3 parameters: the address of two?dimensional array of type int, the # of rows and # columns. It should calculate the sum of the squares of the elements i.e., array ?nums?
23 12 14 3
31 25 41 17
the function could be called

sumsquare( nums, 2, 4 );
Return a value 4434.


b. Modify the ?Date program#4? prgm above by having the user enter the original form for the date as a command line parameter.


#6 Write a prgm that will change every occurrence of one string in a text file to another string Both strings and name should appear as command line arguments. If the program is named ?change?, the invocation ?change Smith Johnson namefile? should open the file ?namefile? and change every occurrence of the string? Smith? to the string ?Johnson?.

PlayMovie.c

#include <Type.h>
#include <Traps.h>
#include <Menus.h>
#include <Fonts.h>
#include <Packages.h>
#include <GestaltEqu.h>

#inlude "Movies.h"
#include "ImageCompression.h"
#define doTheRightThing 5000

int main (void)
{
WindowPtr aWindow;
Rect movieBox;
Boolean done = false;
OSErr err;
EventRecord theEvent;
WindowPtr whichWindow;
short part;

InitGraf (&qd.thePort);
InitFonts ();
InitMenus ();
TEInit ();
InitDialogs (nil);
err = EnterMovies ();
if (err) return;

SetRect (&windowRect, 100, 100, 200, 200 );
aWindow = NewCWindow (nil, &WindowRect, "\pMovie",
false, noGrowDocProc, (WindowPtr)-1, true, 0);

SetPort (aWindow);
aMovie = GetMovie ();
if (aMovie == nil) return;

GetMovieBox (aMovie, &movieBox);
OffsetRect (&movieBox, -movieBox.left, -movieBox.top);
SetMovieBox (aMove, &movieBox);

SizeWindow (aWindow, movieBox.right, movieBox.bottom, true);
ShowWindow (aWindow);

SetMovieGworld (amovie, (CGrafPtr)aWindow, nil);

StartMovie (aMovie);
while ( !IsMovieDone(aMovie) && !done )
{
if (WaitNextEvent (everyEvent, &theEvent, 0, nil))
{
switch (theEvent.what )
{
case updateEvt: whichWindow = (WindowPtr)theEvent.message;
if (whichWindow == aWindow);
{
BeginUpdate (whichWindow);
UpdateMovie (aMovie);
SetPort (whichWindow);
EraseRect (&whichWindow->portRect);
EndUpdate (whichWindow);
}
break;

case mouseDown: part = FindWindow (theEvent.where, &whichWindow);
if (whichWindow == aWindow)
{
switch (part)
{
case inGoAway:
done = TrackGoAway (whichWindow, theEvent.where);
break;
case inDrag: DragWindow (whichWindow, theEvent.where, &qd.screenBits.
bounds);
break;
}

}
break;
}

}
MoviesTask (aMovie, DoTheRightThing);
}
DisposeMovie (aMovie);
DisposeWindow (aWindow);
}
return 0;
0
Comment
Question by:flamyes
  • 5
  • 3
9 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 6155650
>> These are not homework. Just trying to execute in C for
>> the purpose of using on my Web site
>> This is a program that should output a bank statement.
You've got a web site that simulates a bank.

You're designing a web site and must use recusive functions in the design even though you don't understand them?

You bank web site needs to simulate a deck of cards?  

How stupid do you think we are?    

********************************************

We cannot do your schoolwork for you.  That is unethical and is grounds for removal from this site.  (for both you and the experts involved.)  

We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6155684
Don't ever use #define in C++ to define constants.   There are many problems with#define.  Use a true constant instead, like

const int MAX_ARRAY = 512;

instead of

#define MAX_ARRAY 512 /*Const Declaration*/

this is true for all  your defines.  

The code is very hard to follow.  Does it work correctly?  Do you have specific questiosn about it?



>>  must write a recursive function that calculates the n-th power of a floating point value x
The trick to recustion, is to try to break the problem up into two peices.  One small pieace that you can solve and a 2nd peice that is like the original problem, but a little simpler.  This simpler version of the original prooblem can be handled by the recusive call.  

consider  X^4

That is also

X*X^3;

See how that is broken into two peices.  One is X, which will be passed as a paramter to the function, so it is easy (very easy) to evaluate.  The other peices is X^3, which is a simpler version of the orignal problem   you can pass that onto a recsive call of the function.

Note also recursion must end sometime.  So there must be a case that does not get broken into two, but is instead only the simple peice and the other more complex peice actually is not needed.  What would that be in this case?

Can you try to write some code now?
0
 
LVL 2

Accepted Solution

by:
fedor earned 50 total points
ID: 6155690
I don't understand how the movie program is related... Will it run one the same website? Or it's purpose is to convince us that the question "b. Modify the ?Date program..." supposed to help in building a large banking transaction website? (with a deck of cards.. I like this one :-)
0
 
LVL 22

Expert Comment

by:nietod
ID: 6155711
>> a. I must write a structure (struct card) that will represent al full deck of playing cards (struct
>> card deck[52], both the suits(hearts spades, etc) and ranks(A,K,Q,J,10,9,8,etc).
I don't see even an attempt to start this.

How would you define the structure?  How will you represent the suits?  Are you familiar with "enum"?   What about the ranks?  

How would you represent the deck?   The deck stores 52 cards in a particular order....




>> a. Write a struct declaration ( struct string ) . The fields should be named ?chars? and ?length?.
>> b. A strlen() version called mystrlen().
>> c. A strcat() version called mystrsrcat()
>> d. ?print_string? function that takes a ?struct string? as a parameter & outputs the characters to the
>> screen.
>>  e. Put your code for parts a,b,c, and d in a file named ?mystring.h?, and make sure the code works with:
You've done d, but how about a, b, and c.  You need to show us that.


>> Write a program that accepts input of the data in the form of 1/14/94 and outputs it in the form January
>> 14, 1994. Plan the communication between parts of programs before you start coding.
What have you done so far?    

>> #6 Write a prgm that will change every occurrence of one string in a text file to another string
The code you posted has absolutely nothing to do with this.   Did you think we would consider that a good start and finish it for you?   You need to make an effort.  Or at least ask intelligent, focused questions.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Expert Comment

by:nietod
ID: 6155716
>> I don't understand how the movie program is related... Will it run one the same website?
There is no web site.   This is clearly homework.
0
 
LVL 2

Expert Comment

by:fedor
ID: 6155967
nietod, thats is my point too.. obviously, that is not a website with a deck of cards :-))
just curiosity why the movie program was included with the assignment text in the question.
0
 

Author Comment

by:flamyes
ID: 6156920
Dear Nietod & Fedor,

Thank you for your prompt reply!

These prog's are from a book purchased at Borden books(C by Discovery). The movie is from a friend. The website is an Adult site H2KVideo.com. Could not mention due to policy constaints on these types of sites.

Anyhow in an effort to make this site less reliant on Java/Perl rountines, I am exploring C/C++ as a viable alternative to some. Thus I was asked by my supervisor learn how to execute these programs,so as to eventually use these routine in larger programs.

We agree that for the web environment C/c++ may not be as effective for what we're trying to accomplish, but they should be explore.
0
 
LVL 2

Expert Comment

by:fedor
ID: 6157117
But why you didn't accepted nietod's comment as an answer?!! He really tryed to help you, whereas I've only tryied to provide some unsuccessful kind of joke... Those points must go to nietod!

 Regarding your last comment - for my opinion, it is not actually important what is the language/environment you are using (except pathologic cases, such as writing device drivers in JavaScript). The important thing is the algorithmic/approach background and the experience.  

 Obviously, experienced Java/Perl/Server programmer must be able to complete the things above in relatively short time, just using the book to catch the syntax. At least the power and date routines...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6157193
I hope you won't be expecting to get any help now or in the future.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now