473,804 Members | 3,094 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Critique invited


Recently I was seized by the desire to "rotate" the following text:

agdtk
laeha
pmlep
hmttp
aaaaa

into this:

alpha
gamma
delta
theta
kappa

After several detours, some of which were beset by sundry glitchings,
this is what I have:

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

char * app_char (char c);

int main (int argc, char *argv[]) {
/* "rotate" vertically transcribed words to horizontal ones */
/* using a append_char function */

FILE *ifp; FILE *ofp;
char *line;
char *letters[5];
int i; int j;
char * temp = malloc (2 * sizeof(char));
char c = 'i';
int nrows = 6; int ncols = 6;

char **wordlist = (char **) malloc( nrows * sizeof(char *));
for (i = 0; i < 7; i++ ) {
wordlist[i] = (char *)malloc (ncols *sizeof(char));
}

if ( ((ifp = fopen (argv[1], "r")) == NULL) ||
((ofp = fopen (argv[2], "w")) == NULL) ) {
printf ("can\'t open file\n");
exit (1);

}

while (!feof(ifp)) {
if (fscanf(ifp, "%s", line) != 1) {
break; }
printf ("the first letter in %s is %s\n", line, app_char(line[0]));
for (j = 0; j < 5; j++) {
temp = app_char(line[j]);
strcat (wordlist[j], temp);
}

} /* end of feof/fscanf loop */
/* the following is not C it was */
/* */
/* for $j (0..4) { */
/* $newlist[$j] .= $mline[$j]; */
/* */
/* how I arrived at the solution */
/* terminate the strings */
for (i =0; i < 5; i++) {
wordlist[i][6] = '\0';
}

for (j = 0; j < 5; j++) {
printf (" %s \n", wordlist[j]);
}

fclose (ifp); fclose (ifp);
return 0;
}

char * app_char (char c) {
char h = c;
char * twochar = malloc (2 * sizeof (char));
twochar[0] = h;
twochar[1] = '\0';
return twochar;
}

Any ideas, criticisms, etc. invited. Regards T.
Nov 14 '05 #1
8 1431
On Wed, 26 May 2004 04:17:37 GMT, "Tiro Verus"<ti****** *@yahoo.com>
wrote:

Recently I was seized by the desire to "rotate" the following text:

agdtk
laeha
pmlep
hmttp
aaaaa

into this:

alpha
gamma
delta
theta
kappa

After several detours, some of which were beset by sundry glitchings,
this is what I have:

#include<stdio .h>
#include<stdli b.h>
#include<strin g.h>

char * app_char (char c);

int main (int argc, char *argv[]) {
/* "rotate" vertically transcribed words to horizontal ones */
/* using a append_char function */

FILE *ifp; FILE *ofp;
char *line;
char *letters[5];
int i; int j;
char * temp = malloc (2 * sizeof(char));
char c = 'i';
int nrows = 6; int ncols = 6;

char **wordlist = (char **) malloc( nrows * sizeof(char *));
Don't cast the return from malloc. It doesn't help and can hurt by
suppressing diagnostics you would like to see.
for (i = 0; i < 7; i++ ) {
wordlist[i] = (char *)malloc (ncols *sizeof(char));
}
All calls to malloc should be checked for success.
if ( ((ifp = fopen (argv[1], "r")) == NULL) ||
((ofp = fopen (argv[2], "w")) == NULL) ) {
printf ("can\'t open file\n");
exit (1);
Using EXIT_FAILURE instead of 1 will make you code more portable.

}

while (!feof(ifp)) {
feof does not tell you that the file is out of data until AFTER you
attempt to read beyond the last character.
if (fscanf(ifp, "%s", line) != 1) {
line is a pointer that was never initialized to point anywhere. This
invokes undefined behavior. You are not allowed to evaluate any
uninitialized variable.
break; }
printf ("the first letter in %s is %s\n", line, app_char(line[0]));
for (j = 0; j < 5; j++) {
temp = app_char(line[j]);
temp originally held the address of the area you allocated. You have
now lost that address and created a memory leak.
strcat (wordlist[j], temp);
wordlist[j] points to an uninitialized dynamically allocated area. It
does not point to a string. Therefore, you cannot use it as the
target address for strcat. This also invokes undefined behavior.
}

} /* end of feof/fscanf loop */
/* the following is not C it was */
/* */
/* for $j (0..4) { */
/* $newlist[$j] .= $mline[$j]; */
/* */
/* how I arrived at the solution */
/* terminate the strings */
for (i =0; i < 5; i++) {
wordlist[i][6] = '\0';
wordlist[i][6] does not exist. Each wordlist[i] points to an area
capable of holding 6 char. These are wordlist[i][0] through
wordlist[i][5]. This also invokes undefined behavior.
}

for (j = 0; j < 5; j++) {
printf (" %s \n", wordlist[j]);
}

fclose (ifp); fclose (ifp);
return 0;
}

char * app_char (char c) {
char h = c;
char * twochar = malloc (2 * sizeof (char));
You create numerous 2 char allocations but never free any of them.
twochar[0] = h;
twochar[1] = '\0';
return twochar;
}

Any ideas, criticisms, etc. invited. Regards T.


<<Remove the del for email>>
Nov 14 '05 #2
Barry Schwarz wrote:
On Wed, 26 May 2004 04:17:37 GMT, "Tiro Verus"<ti****** *@yahoo.com>
wrote:

Recently I was seized by the desire to "rotate" the following text:

agdtk
laeha
pmlep
hmttp
aaaaa

into this:

alpha
gamma
delta
theta
kappa

After several detours, some of which were beset by sundry glitchings,
this is what I have:

#include<stdi o.h>
#include<stdl ib.h>
#include<stri ng.h>

char * app_char (char c);

int main (int argc, char *argv[]) {
/* "rotate" vertically transcribed words to horizontal ones */
/* using a append_char function */

FILE *ifp; FILE *ofp;
char *line;
char *letters[5];
int i; int j;
char * temp = malloc (2 * sizeof(char));
char c = 'i';
int nrows = 6; int ncols = 6;

char **wordlist = (char **) malloc( nrows * sizeof(char *));

Don't cast the return from malloc. It doesn't help and can hurt by
suppressing diagnostics you would like to see.

for (i = 0; i < 7; i++ ) {
wordlist[i] = (char *)malloc (ncols *sizeof(char));
}

All calls to malloc should be checked for success.
if ( ((ifp = fopen (argv[1], "r")) == NULL) ||
((ofp = fopen (argv[2], "w")) == NULL) ) {
printf ("can\'t open file\n");
exit (1);

Using EXIT_FAILURE instead of 1 will make you code more portable.

}

while (!feof(ifp)) {

feof does not tell you that the file is out of data until AFTER you
attempt to read beyond the last character.

if (fscanf(ifp, "%s", line) != 1) {

line is a pointer that was never initialized to point anywhere. This
invokes undefined behavior. You are not allowed to evaluate any
uninitialized variable.

break; }
printf ("the first letter in %s is %s\n", line, app_char(line[0]));
for (j = 0; j < 5; j++) {
temp = app_char(line[j]);

temp originally held the address of the area you allocated. You have
now lost that address and created a memory leak.

strcat (wordlist[j], temp);

wordlist[j] points to an uninitialized dynamically allocated area. It
does not point to a string. Therefore, you cannot use it as the
target address for strcat. This also invokes undefined behavior.

}

} /* end of feof/fscanf loop */
/* the following is not C it was */
/* */
/* for $j (0..4) { */
/* $newlist[$j] .= $mline[$j]; */
/* */
/* how I arrived at the solution */
/* terminate the strings */
for (i =0; i < 5; i++) {
wordlist[i][6] = '\0';

wordlist[i][6] does not exist. Each wordlist[i] points to an area
capable of holding 6 char. These are wordlist[i][0] through
wordlist[i][5]. This also invokes undefined behavior.

}

for (j = 0; j < 5; j++) {
printf (" %s \n", wordlist[j]);
}

fclose (ifp); fclose (ifp);
return 0;
}

char * app_char (char c) {
char h = c;
char * twochar = malloc (2 * sizeof (char));

You create numerous 2 char allocations but never free any of them.

twochar[0] = h;
twochar[1] = '\0';
return twochar;
}

Any ideas, criticisms, etc. invited. Regards T.



<<Remove the del for email>>

--
<<Remove the del for email>>

Nov 14 '05 #3
Tiro Verus wrote:

Recently I was seized by the desire to "rotate" the following text:

agdtk
laeha
pmlep
hmttp
aaaaa

into this:

alpha
gamma
delta
theta
kappa
.... snip ...
Any ideas, criticisms, etc. invited. Regards T.


You need to gather the input lines into something. I suggest a
linked list of objects that look like:

struct object {
size_t lgh;
char *line;
struct object *next;
}

Having collected such a list, you now need a process for
extracting the ith char of an object, with the proviso that the
char is either '\0' or ' ' whenever i exceeds the length of the
objects line:

char ithchar(size_t i, struct object *obj);

which you can use to build the output, possibly into another list
of struct object. So doing would have the advantage of self
checking, in that two transforms should be the identity transform.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #4
In 'comp.lang.c', "Tiro Verus"<ti****** *@yahoo.com> wrote:

Recently I was seized by the desire to "rotate" the following text:

agdtk
laeha
pmlep
hmttp
aaaaa

into this:

alpha
gamma
delta
theta
kappa
<big snip>
Any ideas, criticisms, etc. invited. Regards T.


Sounds complicated to me:

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

#define N(a) (sizeof(a)/sizeof*(a))

int main (void)
{
static char const *as[] =
{
"agdtk",
"laeha",
"pmlep",
"hmttp",
"aaaaa",
};

size_t i;
size_t const width = strlen (*as);

for (i = 0; i < width; i++)
{
size_t j;
for (j = 0; j < N (as); j++)
{
putchar (as[j][i]);
}
putchar ('\n');
}

return 0;
}

D:\CLC\V\VERUS> bc proj.prj
alpha
gamma
delta
theta
kappa

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #5
CBFalconer <cb********@yah oo.com> wrote:

agdtk
laeha
pmlep
hmttp
aaaaa

You need to gather the input lines into something. I suggest a
linked list of objects that look like:

struct object {
size_t lgh;
char *line;
struct object *next;
}

Having collected such a list, you now need a process for
extracting the ith char of an object, with the proviso that the
char is either '\0' or ' ' whenever i exceeds the length of the
objects line:

char ithchar(size_t i, struct object *obj);

which you can use to build the output, possibly into another list
of struct object. So doing would have the advantage of self
checking, in that two transforms should be the identity transform.


Interesting, I was just reading the input from a file. I'm not
really sure if in my original "inspiratio n" all the vertical "lines"
were the same length.

Nov 14 '05 #6
Emmanuel Delahaye <em**********@n oos.fr> wrote:

<snip snip before>
for (i = 0; i < width; i++)
{
size_t j;
for (j = 0; j < N (as); j++)
{
putchar (as[j][i]);
}
putchar ('\n');
}


Thank you, I tried this many times and I gave up.
Nov 14 '05 #7
Emmanuel Delahaye <em**********@n oos.fr> wrote:
In 'comp.lang.c', "Tiro Verus"<ti****** *@yahoo.com> wrote:
Recently I was seized by the desire to "rotate" the following text:

agdtk
laeha
pmlep
hmttp
aaaaa

into this:

alpha
gamma
delta
theta
kappa
<big snip>


ED: Sounds complicated to me:

#define N(a) (sizeof(a)/sizeof*(a)) <snip> for (j = 0; j < N (as); j++)


And this (these 2 lines) aren't complicated????

Nov 14 '05 #8
In 'comp.lang.c', "Tiro Verus"<ti****** *@yahoo.com> wrote:
ED: Sounds complicated to me:

#define N(a) (sizeof(a)/sizeof*(a))

<snip>
for (j = 0; j < N (as); j++)


And this (these 2 lines) aren't complicated????


There are idiomatic. Not complicated at all.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #9

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

Similar topics

3
1740
by: Saqib Ali | last post by:
Hello All, I m not sure if this is the right place to ask for a critique. If not please refer me to another group. Thanks. I would a criqtique of the following website: http://www.xml-dev.com/itnetcentrix/netcentrix.htm Thanks.
19
2556
by: TC | last post by:
Are there any good sites or forums for a web critique? I went to alt.html.critique and it's pretty dead.
9
2286
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much memory, but it should certainly handle 64-bit cpus :) #include <stdio.h> #include <stdlib.h> char * to_binary (unsigned long value) {
188
7264
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection - non-deterministic destructors - Objects can't exist on the stack - Type / Reference Types
39
1944
by: Eric | last post by:
There is a VB.NET critique on the following page: http://www.vb7-critique.741.com/ for those who are interested. Feel free to take a look and share your thoughts. Cheers, Eric. Ps: for those on comp.programming, this may be off topic, but I've posted there because the critique was part of a discussion in that group.
0
10558
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
10318
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...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9130
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...
0
5503
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...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.