why would I change AddWord()? I could understand if I was trying to alphabetize the list but I'm not. I am trying to print it out in order of frequency... Ex suppose there are 5 "A" and 2 "The" it would print A 5 before The 2. The only way I can think to achieve this is to make my list a double linked list and begin swapping elements or copy it into an array and use a temporary array to swap elements...essentially the same thing
Ineedhelpplz -7 Junior Poster in Training
delete line 56 -- it does nothing.
deleted....one addition question. Suppose I wanted to print my results in descending order of frequency. I should modify my while loop that prints the results however I'm not too sure how. Should I write a function that utilizes a sort algorithm and sorts the list, or is there an easier way to just modify something in this while to get the same desired results. Or should I make my list a double linked list so I can navigate through it and recursively print the descending values?
while(pCounter != NULL)
{
show(pCounter);
pCounter = pCounter->pNext;
}
Ineedhelpplz -7 Junior Poster in Training
Final working result:
#include <stdio.h>
#include <string.h>
#define MAX_WORD_LENGTH 31
#define MAX_TEXT_LENGTH 10000
#define TRUE 1
#define FALSE 0
#define BUFSIZE 100
/* Structure defining a count of the occurrences of a given word */
struct WordCounter
{
char *word;
int word_count;
struct WordCounter *pNext; /* Pointer to the next word counter in the list */
};
/* Function prototypes */
void addWord(char *pWord);
/* Adds a word to the list or updates exisiting word */
int is_separator(char ch);
/* Tests for a separator character */
void show(struct WordCounter *pWordcounter);
/* Outputs a word and its count of occurrences */
struct WordCounter* createWordCounter(char *word);
/* Creates a new WordCounter structure */
int getword(char *, int);
/* Self explanitory */
/* Global variables */
struct WordCounter *pStart = NULL;
/* Pointer to first word counter in the list */
int main(int argc, char* argv[])
{
char text[MAX_TEXT_LENGTH];
char buffer[MAX_WORD_LENGTH];
size_t i = 0;
int len = 0;
struct WordCounter *pCounter = NULL;
FILE* fp = fopen("gettysburg.txt", "rt"); /* Opens the text file */
if(fp == NULL)
{
printf("Error: can't open file. \n");
return 1;
}
else
{
printf("File opened successfully. \n");
printf("Calculating frequency of word occurances:\n");
/* Extract the words from the text */
while(fscanf(fp, "%s", buffer) > 0)
{
addWord(buffer); /* Add the word to the list */
buffer[len++];
}
/* List the words and their counts */
pCounter = pStart;
while(pCounter != NULL)
{
show(pCounter);
pCounter = pCounter->pNext;
}
printf("\n");
/* Free the memory that we allocated */ …
Ineedhelpplz -7 Junior Poster in Training
however the last word earth is fully displayed which gives me some hope in properly storing the values I am extracting and not just their first letter haha
Ineedhelpplz -7 Junior Poster in Training
took your advice and made a big mess...
blade71(38)% cat gb
File opened successfully.
Calculating frequency of word occurances:
FsasyaofbfotcannciLadttptamaceNwaeiagcwtwtnoanscasdcleWamoagbfotwWhctdapotfaafrpftwhgtlttnmlIiafaptwsdtBialswcndwcncwcnhtgTbmladwshhcifaopptaodTwwlnnlrwwshbicnfwtdhIifutlrtbdhttuwwtwfhhtfsnaIirfutbhdttgtrbutfthdwtidttcfwtgtlfmodtwhhrttdsnhdivttnuGshanbofatgotpbtpftpsnpftearth
As you can see it is now getting every first letter of the gettysburg address and more. It is not getting the whole string and fgets is acting as getchar for some strange reason. Here is the source:
i
int main(int argc, char* argv[])
{
char text[MAX_TEXT_LENGTH];
char buffer[MAX_WORD_LENGTH];
size_t i = 0;
int len = 0;
struct WordCounter *pCounter = NULL;
FILE* fp = fopen("gettysburg.txt", "rt"); /* Opens the text file */
if(fp == NULL)
{
printf("Error: can't open file. \n");
return 1;
}
else
{
printf("File opened successfully. \n");
int z = 0;
while(!feof(fp))
{
fscanf(fp, "%s", &text[z]);
z++;
}
printf("Calculating frequency of word occurances:\n");
fgets(&text[z],MAX_WORD_LENGTH,fp);
/* Extract the words from the text */
while(text[i] != '\0')
{
/* Skip over separators */
while(is_separator(text[i]))
++i;
/* It is either the end of the string or the start of a word */
/* As long as it is not the string terminator copy the character */
len = 0; /* Reset character count */
while((!is_separator(text[i])) && (text[i] != '\0'))
buffer[len++] = text[i++];
if(len>0) /* Check we have some characters in the word */
{
buffer[len] = '\0'; /* We reached the end of a word so add terminator *
/
addWord(buffer); /* Add the word to the list */
}
}
}
/* List the words and their counts */
pCounter = pStart;
while(pCounter != NULL) …
Ineedhelpplz -7 Junior Poster in Training
So everything in my program works except for the main itself. The program is supposed to take in a file provided through standard input and print out the descending order of words followed by the frequency that they occur. I can do this individually line for line but am having trouble putting it together as a whole to count all the lines and not just one specific line. Here is my code:
blade71(382)% cat test.c
#include <stdio.h>
#include <string.h>
#define MAX_WORD_LENGTH 31
#define MAX_TEXT_LENGTH 10000
#define TRUE 1
#define FALSE 0
#define BUFSIZE 100
/* Structure defining a count of the occurrences of a given word */
struct WordCounter
{
char *word;
int word_count;
struct WordCounter *pNext; /* Pointer to the next word counter in the list */
};
/* Function prototypes */
void addWord(char *pWord);
/* Adds a word to the list or updates exisiting word */
int is_separator(char ch);
/* Tests for a separator character */
void show(struct WordCounter *pWordcounter);
/* Outputs a word and its count of occurrences */
struct WordCounter* createWordCounter(char *word);
/* Creates a new WordCounter structure */
int getword(char *, int);
/* Self explanitory */
int getch(void);
void ungetch(int);
/* Global variables */
struct WordCounter *pStart = NULL;
/* Pointer to first word counter in the list */
char buf[BUFSIZE];
int bufp = 0;
main()
{
char text[MAX_TEXT_LENGTH]; /* Stores input text */
char buffer[MAX_WORD_LENGTH]; /* Buffer to hold a word */
size_t i = 0; /* Index to text */
int len …
Ineedhelpplz -7 Junior Poster in Training
suppose I have:
#define special_for(X,Y) { register int i = 0; for (;i<X;i++) {Y} }
It is used in the following functions:
init_lineholder(int nlines)
{
special_for(nlines, lineptr[i] = (char *)malloc(MAXLEN); tmp++;)
}
AND
print_lines()
{
special_for(tmp, if (lineptr[(first+tmp-i)%tmp] == NULL) break; else
printf("%s\n", lineptr[(first+tmp-i)%tmp]) ; ) /* had to space bcuz of stupid smiley */
}
how do I rewrite the functions so I don't need to use the special_for macro?
Ineedhelpplz -7 Junior Poster in Training
83 and no response....does it look right? can someone at least point out what I'm doing wrong. Do my algorithms make sense?
Ineedhelpplz -7 Junior Poster in Training
So I have an assignment that I have been attempting for 2 weeks now and the class has been given extensions twice now. Everyone seems to be struggling with this part of the assignment. The assignment is to do K&R Exercise 5-13, unix tail command.
Instructions: "The program takes lines from standard input and keeps the last n of them in memory as it goes through standard input. When it gets to an EOF, it prints the last n lines out. You may assume n is less than 2000 and each individual line is not longer than 1000 including the newline and the null terminator.
Use two source files plus a header file for this, just to show you know how to make multiple source files work together.
tail.c:
interprets the command line argument.
Calls init_lineholder(int nlines) with the numebr from the command line.
Does a loop calling getline and insert_line(char *line)
when getline returns 0 (indicating EOF on stdin), it calls print_lines().
lineholder.c
contains a static array of pointers for lines.
Implements init_lineholder, insert_line, and print_lines.
Init_lineholder initializes the "first" slot and related variables.
Insert_line adds a line to the array.
It must allocate memory for the new line.
It must free the memory for a line no longer needed, if any.
Print_lines prints the lines in the array and frees the memory used for them.
lineholder.h: Just has prototypes for the three …
Ineedhelpplz -7 Junior Poster in Training
Thanks! Got my answer:
292 16 % 292 16 / + 16 * 292 ^ ~
Ineedhelpplz -7 Junior Poster in Training
So I have written a reverse polish notation calculator:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#define NUMBER '0'
#define MAXOP 100
int main ()
{
int type;
int op1, op2;
char s[MAXOP];
while ((type = getop(s)) != EOF)
switch (type)
{
case NUMBER:
push(atoi(s));
break;
case '^':
push ((unsigned int) pop() ^ (unsigned int) pop());
break;
case '~':
push(~(unsigned int) pop());
break;
case '+':
case '*':
if (type == '+')
push(pop() + pop());
else
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
case '%':
if ((op2 = pop()) != 0.0) {
if (type == '/')
push(pop() / op2);
else {
op1 = pop();
push(op1 - op2 * ((int) (op1/op2)));
}
} else
printf("Error: Zero divisor!\n");
break;
case '\n':
printf("The answer is %d\n", pop());
break;
default:
printf("Error: Unknown command %s!\n", s);
break;
}
return 0;
}
I now need to run the infix string " ~(((202%16) + (292/16)*16) ^292 " through my calculator...however the string is in infix, RPN takes postfix. I have been having trouble converting this expression to postfix and need help. The program itself is done I just want to test that input and don't quite understand how to make it work in postfix.
Ineedhelpplz -7 Junior Poster in Training
Completed entirely:
blade71(60)% cat xbits.c
/*
* stubs for functions to study
* integer-hex conversions
*
*/
#include "xbits.h"
/* Helpful function to get around not being able to use strchr()
* Converts hex to decimal value
*/
int hex_Val(int c)
{
char hex_values[] = "aAbBcCdDeEfF";
int i;
int answer = 0;
for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
{
if (hex_values[i] == c)
{
answer = 10 + (i/2);
}
}
return answer;
}
/* function represents the int n as a hexstring which it places in the
hexstring array */
void itox(char hexstring[], int n)
{
int b = 0, TEST = 0, index =0, r[100], i=0;
printf("in itox, processing %d\n",n);
hexstring[i++] = '0';
hexstring[i++] = 'x';
for (index = 0; n>=16; index++)
{
r[index] = n %16;
n = n/16;
}
r[index] = n;
r[index+1] = '\0';
for (;index>=0;index--)
{
if (r[index] == 0)
{
hexstring[i] = '0';
i++;
}
else if (r[index] == 1)
{
hexstring[i] = '1';
i++;
}
else if (r[index] == 2)
{
hexstring[i] = '2';
i++;
}
else if (r[index] == 3)
{
hexstring[i] = '3';
i++;
}
else if (r[index] == 4)
{
hexstring[i] = '4';
i++;
}
else if (r[index] == 5)
{
hexstring[i] = '5';
i++;
}
else if (r[index] == 6)
{
hexstring[i] = '6';
i++;
}
else if (r[index] == 7)
{
hexstring[i] = '7';
i++;
}
else if (r[index] == 8)
{
hexstring[i] = '8';
i++;
}
else if (r[index] == 9) …
Ineedhelpplz -7 Junior Poster in Training
Corrected:
void itox(char hexstring[], int n)
{
int b = 0, TEST = 0, index =0, r[100], i=0;
printf("in itox, processing %d\n",n);
hexstring[i++] = '0';
hexstring[i++] = 'x';
for (index = 0; n>=16; index++)
{
r[index] = n %16;
n = n/16;
printf("Value of n divided by 16: %d\n",n);
}
r[index] = n;
for (;index>=0;index--)
{
if (r[index] == 0)
{
hexstring[i] = '0';
i++;
}
else if (r[index] == 1)
{
hexstring[i] = '1';
i++;
}
else if (r[index] == 2)
{
hexstring[i] = '2';
i++;
}
else if (r[index] == 3)
{
hexstring[i] = '3';
i++;
}
else if (r[index] == 4)
{
hexstring[i] = '4';
i++;
}
else if (r[index] == 5)
{
hexstring[i] = '5';
i++;
}
else if (r[index] == 6)
{
hexstring[i] = '6';
i++;
}
else if (r[index] == 7)
{
hexstring[i] = '7';
i++;
}
else if (r[index] == 8)
{
hexstring[i] = '8';
i++;
}
else if (r[index] == 9)
{
hexstring[i] = '9';
i++;
}
else if (r[index] == 10)
{
hexstring[i] = 'A';
i++;
}
else if (r[index] == 11)
{
hexstring[i] = 'B';
i++;
}
else if (r[index] == 12)
{
hexstring[i] = 'C';
i++;
}
else if (r[index] == 13)
{
hexstring[i] = 'D';
i++;
}
else if (r[index] == 14)
{
hexstring[i] = 'E';
i++;
}
else if (r[index] == 15)
{
hexstring[i] = 'F';
i++;
}
else
{
printf("Err %d\n", r[index]);
}
}
return;
}
xtoi is now in an endless loop during the execution; because I never end hexstring with '\0'. That is the next thing I will be working on
Ineedhelpplz -7 Junior Poster in Training
Please help me find the endless loop and how to fix it:
blade71(555)% showxbits_2
32
in itox, processing 32
Test Value (b div 16): 2
Value of b: 32
Value of b (# mod 16): 0
/*
* stubs for functions to study
* integer-hex conversions
*
*/
#include "xbits.h"
/* Helpful function to get around not being able to use strchr()
* Converts hex to decimal value
*/
int hex_Val(int c)
{
char hex_values[] = "aAbBcCdDeEfF";
int i;
int answer = 0;
for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
{
if (hex_values[i] == c)
{
answer = 10 + (i/2);
}
}
return answer;
}
/* function represents the int n as a hexstring which it places in the
hexstring array */
void itox( char hexstring[], int n)
{
printf("in itox, processing %d\n",n);
hexstring[0] = '0';
hexstring[1] = 'x';
int b,c=0, i=0, TEST = 0;
b=n;
while (b>=16)
{
TEST = b/16;
hexstring[i+2]=TEST;
i++;
printf("Test Value (b div 16): %d\n", TEST);
printf("Value of b: %d\n", b);
b = b%16;
hexstring[i+2] = b;
printf("Value of b (# mod 16): %d\n", b);
i++;
}
while ( b>=0 && b<15 )
{
int useless = b;
if (hexstring[i+2]==10)
hexstring[i+2] = 'A';
else if (hexstring[i+2]==11)
hexstring[i+2] = 'B';
else if (hexstring[i+2]==12)
hexstring[i+2] = 'C';
else if (hexstring[i+2]==13)
hexstring[i+2] = 'D';
else if (hexstring[i+2]==14)
hexstring[i+2] = 'E';
else if (hexstring[i+2]==15)
hexstring[i+2] = 'F';
else
hexstring[i+2]=hexstring[i+2];
useless--;
b=b-useless;
}
return;
}
/* function converts hexstring …
Ineedhelpplz -7 Junior Poster in Training
Solved! :
blade71(42)% gcc showxbits.c xbits_2.c -o showxbits
blade71(43)% showxbits
in itox, processing 703557
in xtoi, processing ABC45
703557 ABC45 703557
blade71(44)% cat xbits_2.c
/*
* stubs for functions to study
* integer-hex conversions
*
*/
#include "xbits.h"
/* Helpful function to get around not being able to use strchr()
* Converts hex to decimal value
*/
int hex_To_dec(int c)
{
char hex_values[] = "aAbBcCdDeEfF";
int i;
int answer = 0;
for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
{
if (hex_values[i] == c)
{
answer = 10 + (i/2);
}
}
return answer;
}
/* function represents the int n as a hexstring which it places in the
hexstring array */
void itox( char hexstring[], int n)
{
printf("in itox, processing %d\n",n);
hexstring[0] = '0';
hexstring[1] = 'x';
int b,c=0, i=2;
b=n;
while (b>15)
{
hexstring[i]=b%16;
b=b/16;
i++;
c++;
}
hexstring[i]=b;
for (i=c;i>=0;--i)
{
if (hexstring[i]==10)
hexstring[i] = 'A';
else if (hexstring[i]==11)
hexstring[i] = 'B';
else if (hexstring[i]==12)
hexstring[i] = 'C';
else if (hexstring[i]==13)
hexstring[i] = 'D';
else if (hexstring[i]==14)
hexstring[i] = 'E';
else if (hexstring[i]==15)
hexstring[i] = 'F';
else
hexstring[i]=hexstring[i];
}
return;
}
/* function converts hexstring array to equivalent integer value */
int xtoi(char hexstring[])
{
printf("in xtoi, processing %s\n", hexstring);
int answer = 0;
int i = 0;
int valid = 1;
int hexit;
if (hexstring[i] == '0')
{
++i;
if (hexstring[i] == 'x' || hexstring[i] == 'X')
{
++i;
}
}
while(valid && hexstring[i] != '\0')
{
answer = answer * 16; …
Ineedhelpplz -7 Junior Poster in Training
Here is a new and improved version of itox that still does not work:
blade71(175)% gcc showxbits.c test.c -o showxbits
Undefined first referenced
symbol in file
xtoi /var/tmp//ccYWJ1Yj.o
ld: fatal: Symbol referencing errors. No output written to showxbits
collect2: ld returned 1 exit status
blade71(176)% cat test.c
#include <stdio.h>
#include "xbits.h"
void itox( char hexstring[], int n)
{
printf("in itox, processing %d\n",n);
int number;
number = n;
int i = 2;
hexstring[0] = '0';
hexstring[1] = 'x';
while (number != 0)
{
number = number % 16;
if (number = 1)
{
hexstring[i] = '1';
i++;
}
else if (number = 2)
{
hexstring[i] = '2';
i++;
}
else if (number = 3)
{
hexstring[i] = '3';
i++;
}
else if (number = 4)
{
hexstring[i] = '4';
i++;
}
else if (number = 5)
{
hexstring[i] = '5';
i++;
}
else if (number = 6)
{
hexstring[i] = '6';
i++;
}
else if (number = 7)
{
hexstring[i] = '7';
i++;
}
else if (number = 8)
{
hexstring[i] = '8';
i++;
}
else if (number = 9)
{
hexstring[i] = '9';
i++;
}
else if (number = 10)
{
hexstring[i] = 'A';
i++;
}
else if (number = 11)
{
hexstring[i] = 'B';
i++;
}
else if (number = 12)
{
hexstring[i] = 'C';
i++;
}
else if (number = 13)
{
hexstring[i] = 'D';
i++;
}
else if (number = 14)
{
hexstring[i] = 'E';
i++;
}
else if (number = 15)
{
hexstring[i] = 'F';
i++;
}
else if (number = 0)
{
hexstring[i] = '\0';
i++;
}
}
}
In addition this problem could have been done using an enum instead of a long drawn-out if statement.
Ineedhelpplz -7 Junior Poster in Training
Here is a thread from a month or so ago of me working through the same problem.
http://www.daniweb.com/forums/thread226007.html
The solution is listed and works
In case you don't care to take a look:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
/* This program will remove all trailing white space from an input file.
Using a loop containing several if statements to check for the ASCII
value of any white space generating characters; the program will
individually access each character until it reaches the EOF. */
int read(int index)
{
if (index < MAXLINE - 1)
return index + 1;
else
return 0;
}
int main(void)
{
char S[MAXLINE];
int head, tail, White_Space, Return_Value, c;
Return_Value = White_Space = head = tail = 0;
while ((c = getchar()) != EOF)
{
if (c == '\n')
{
head = tail = 0;
if (White_Space)
putchar('\n');
White_Space = 0;
}
else if (c == ' ' || c == '\t')
{
if (read(head) == tail)
{
putchar(S[tail]);
tail = read(tail);
White_Space = 1;
Return_Value = EXIT_FAILURE;
}
S[head] = c;
head = read(head);
}
else
{
while (head != tail)
{
putchar(S[tail]);
tail = read(tail);
}
putchar(c);
White_Space = 1;
}
}
return Return_Value;
}
Ineedhelpplz -7 Junior Poster in Training
I have already completed my hex to integer conversion function which works properly with the provided driver. However, my integer to hex function gets stuck in an infinite loop and I have no clue why. The red section of my code is the function which is giving me problems. Everything else works; however, is a rough draft so please don't give me crap about my other functions or drivers. They are all under construction. The Second code is my instructors driver he provided my class with. The line in green is a line I changed to allocate the correct memory size.
Thank You!
blade71(106)% cat xbits.c
/*
* stubs for functions to study
* integer-hex conversions
*
*/
#include "xbits.h" /* Just prototypes...same as file */
/* Helpful function to get around not being able to use strchr()
* Converts hex to decimal value
*/
int hex_To_dec(int c)
{
char hex_values[] = "aAbBcCdDeEfF";
int i;
int answer = 0;
for (i=0; answer == 0 && hex_values[i] != '\0'; i++)
{
if (hex_values[i] == c)
{
answer = 10 + (i/2);
}
}
return answer;
}
/* function represents the int n as a hexstring which it places in the
hexstring array */
void itox( char hexstring[], int n)
{
printf("in itox, processing %d\n",n);
int number;
n = number;
int i;
hexstring[0] = '0';
hexstring[1] = 'x';
while (number != '0')
{
i = 2;
hexstring[i] = (number%16) < 10 ? (number%16) : 'A' + (number%16) - 10;
i++; …
Ineedhelpplz -7 Junior Poster in Training
Updated: Problem solved...however in C it appears logical to derive; when you solve a problem usually you are solving it with another problem. Alas my program works as intended. However, now it is somehow reading 3 ASCII characters that do not exist in the vt.in file. Check out my execution and octal dumps.
Script started on Wed Sep 30 10:19:21 2009
blade71(1)% gcc -o vt visitype.c
blade71(2)% vt<vt.in>vt.out
blade71(3)% cat vt.in
}~a,bzZ
blade71(4)% cat vt.out
The code 1 represents SOH
The code 2 represents STX
The code 7d represents }
The code 7e represents ~
The code 61 represents a
The code 2c represents ,
The code 62 represents b
The code 7a represents z
The code 5c represents \
The code 7f represents DEL
The code 5a represents Z
The code a represents NL
The code 0 represents NUL
The code 3b represents ;
The code 73 represents s
The code ffffff80 represents
blade71(5)% where is the extra 3b, 73, and value ffffff80 coming from?
blade71(6)% od -x vt.in
0000000 0102 7d7e 612c 627a 5c7f 5a0a
0000014
blade71(7)% od -x vt.out
0000000 5468 6520 636f 6465 2020 2031 2072 6570
0000020 7265 7365 6e74 7320 534f 480a 5468 6520
0000040 636f 6465 2020 2032 2072 6570 7265 7365
0000060 6e74 7320 …
Ineedhelpplz -7 Junior Poster in Training
bump....plz don't give negative feedback for this. I waited over 24 hours before bumping
Ineedhelpplz -7 Junior Poster in Training
The problem is char = 1 byte; int = 4 (sometimes 8 depends on system) bytes. The assignment:
k=s
is either ambiguous or just incorrect. I'm not sure which....How do I either change it to K=S + in order to have them be converted to int type? Do I need another function to convert the program or is there some kind of typecasts I can use?
My error test displays the correct bits in S[] however, because it is type char and not int they spill over into the next element...producing the wrong answer.
Err Test 2: 1
Err Test 2: 2
Err Test 2: 7d // Recall this from the octal dump
Err Test 2: 7e // done earlier...it is the correct value
Err Test 2: 61
Err Test 2: 2c
Err Test 2: 62
Err Test 2: 7a
Err Test 2: 5c
Err Test 2: 7f
Err Test 2: 5a
Err Test 2: a
The code 55 represents U
The code 56 represents V
The code 53 represents S
The code 41 represents A
The code 20 represents SP
The code 4e represents N
The code 53 represents S
The code 45 represents E
The code 0 represents NUL
The code 4e represents N
The code 58 represents X
The code 4e represents N
The code 0 represents NUL
The code 0 represents NUL
The code 0 represents NUL
Ineedhelpplz -7 Junior Poster in Training
Why am I getting the wrong output? I have a feeling it has something to do with using type char vs. int....but im not specifically too sure. Please help!
1.
blade71(130)% gcc -o vt visitype.c
2.
blade71(131)% vt < vt.in > vt.out
3.
blade71(132)% cat vt.out
4.
The code 55 represents U
5.
The code 4c represents L
6.
The code 56 represents V
7.
The code 53 represents S
8.
The code 41 represents A
9.
The code 20 represents SP
10.
The code 4e represents N
11.
The code 53 represents S
12.
The code 45 represents E
13.
The code 0 represents NUL
14.
The code 4e represents N
15.
The code 58 represents X
16.
blade71(133)% cat vt.in
17.
}~a,bzZ
18.
blade71(134)% od -x vt.in
19.
0000000 0102 7d7e 612c 627a 5c7f 5a0a
20.
0000014
21.
blade71(135)% cat visitype.c
22.
#include <stdio.h>
23.
#define MAX 1000
24.
25.
void aVal(char asciiname[], char S[]);
26.
27.
main ()
28.
{
29.
int i, c, j;
30.
char asciiname[] =
31.
"NUL\0" "SOH\0" "STX\0" "ETX\0" "EOT\0" "ENQ\0" "ACK\0" "BEL\0"
32.
" BS\0" " HT\0" " NL\0" " VT\0" " NP\0" " CR\0" " SO\0" " SI\0"
33.
"DLE\0" "DC1\0" "DC2\0" "DC3\0" "DC4\0" "NAK\0" "SYN\0" "ETB\0"
34.
"CAN\0" " EM\0" "SUB\0" "ESC\0" " FS\0" " GS\0" " RS\0" " VS\0"
35.
" SP\0" " !\0" " \"\0" " #\0" " $\0" " %\0" " &\0" " '\0"
36.
" (\0" " )\0" " *\0" " +\0" " ,\0" …
Ineedhelpplz -7 Junior Poster in Training
After using printf I realized my output is in fact wrong:
blade71(130)% gcc -o vt visitype.c
blade71(131)% vt < vt.in > vt.out
blade71(132)% cat vt.out
The code 55 represents U
The code 4c represents L
The code 56 represents V
The code 53 represents S
The code 41 represents A
The code 20 represents SP
The code 4e represents N
The code 53 represents S
The code 45 represents E
The code 0 represents NUL
The code 4e represents N
The code 58 represents X
blade71(133)% cat vt.in
}~a,bzZ
blade71(134)% od -x vt.in
0000000 0102 7d7e 612c 627a 5c7f 5a0a
0000014
blade71(135)% cat visitype.c
#include <stdio.h>
#define MAX 1000
void aVal(char asciiname[], char S[]);
main ()
{
int i, c, j;
char asciiname[] =
"NUL\0" "SOH\0" "STX\0" "ETX\0" "EOT\0" "ENQ\0" "ACK\0" "BEL\0"
" BS\0" " HT\0" " NL\0" " VT\0" " NP\0" " CR\0" " SO\0" " SI\0"
"DLE\0" "DC1\0" "DC2\0" "DC3\0" "DC4\0" "NAK\0" "SYN\0" "ETB\0"
"CAN\0" " EM\0" "SUB\0" "ESC\0" " FS\0" " GS\0" " RS\0" " VS\0"
" SP\0" " !\0" " \"\0" " #\0" " $\0" " %\0" " &\0" " '\0"
" (\0" " )\0" " *\0" " +\0" " ,\0" " -\0" " .\0" " /\0"
" 0\0" " 1\0" " 2\0" " 3\0" " 4\0" " 5\0" " 6\0" " 7\0"
" 8\0" " 9\0" " :\0" " ;\0" " <\0" " =\0" " >\0" " ?\0"
" @\0" " A\0" " B\0" " C\0" " D\0" " E\0" " F\0" " G\0" …
Ineedhelpplz -7 Junior Poster in Training
...I am getting the desired output. However, I need to use printf in place of putchar to print the value and also have it print the hexadecimal value of j.
Ineedhelpplz -7 Junior Poster in Training
Updated code:
blade71(7)% gcc -o vt visitype.c
blade71(8)% vt<vt.in>vt.out
blade71(9)% od -x vt.in
0000000 0102 7d7e 612c 627a 5c7f 5a0a
0000014
blade71(10)% od -x vt.out
0000000 554c 5653 4120 4e53 4500 4e58
0000014
blade71(11)% cat vt.out
ULVSA NSENXblade71(12)% cat vt.in
}~a,bzZ
blade71(13)% cat visitype.c
#include <stdio.h>
#define MAX 1000
void aVal(char asciiname[], char S[]);
main ()
{
int i, c, j;
char asciiname[] =
"NUL\0" "SOH\0" "STX\0" "ETX\0" "EOT\0" "ENQ\0" "ACK\0" "BEL\0"
" BS\0" " HT\0" " NL\0" " VT\0" " NP\0" " CR\0" " SO\0" " SI\0"
"DLE\0" "DC1\0" "DC2\0" "DC3\0" "DC4\0" "NAK\0" "SYN\0" "ETB\0"
"CAN\0" " EM\0" "SUB\0" "ESC\0" " FS\0" " GS\0" " RS\0" " VS\0"
" SP\0" " !\0" " \"\0" " #\0" " $\0" " %\0" " &\0" " '\0"
" (\0" " )\0" " *\0" " +\0" " ,\0" " -\0" " .\0" " /\0"
" 0\0" " 1\0" " 2\0" " 3\0" " 4\0" " 5\0" " 6\0" " 7\0"
" 8\0" " 9\0" " :\0" " ;\0" " <\0" " =\0" " >\0" " ?\0"
" @\0" " A\0" " B\0" " C\0" " D\0" " E\0" " F\0" " G\0"
" H\0" " I\0" " J\0" " K\0" " L\0" " M\0" " N\0" " O\0"
" P\0" " Q\0" " R\0" " S\0" " T\0" " U\0" " V\0" " W\0"
" X\0" " Y\0" " Z\0" " [\0" " \\\0" " ]\0" " ^\0" " _\0"
" `\0" " a\0" " b\0" " c\0" " d\0" " e\0" " f\0" " g\0"
" h\0" " i\0" " j\0" " k\0" " l\0" " m\0" " n\0" " o\0"
" p\0" " q\0" " r\0" " s\0" " t\0" " u\0" " v\0" " w\0"
" x\0" " y\0" " z\0" " {\0" " |\0" " }\0" " ~\0" "DEL\0"
;
char S[MAX], *p1;
p1 = S;
c = getchar();
for (i = 0; c!=EOF; c=getchar())
{
S[i] = c;
i++;
}
S[i] = '\0';
aVal (asciiname, S);
}
void aVal(char asciiname[], char S[])
{
int i, j;
int k;
i = 0;
k = S[i];
for ( i = 0; k!='\0'; k = S[i])
{
j = asciiname[k];
putchar(j);
i++;
}
S[i] = '\0';
}
as you can see I am getting output, however not the desired output.
Ineedhelpplz -7 Junior Poster in Training
Gees, you really like doing things the hard way. Here's a simple code snippet that outputs the character:hex representations of each character of a string. See if you can incorporate some aspects of the code into your project.
#include <stdio.h> #include <string.h> int main(void) { char str[] = "ABCDEFG"; int i; for (i = 0; i < strlen(str); ++i) { printf("%c:%x ", str[i], str[i]); } return 0; }
The assignment does not allow strlen or str. I can only use getchar and putchar to read/write standard I/O
Ineedhelpplz -7 Junior Poster in Training
error was simple. The function was encapsulated into main rather than outside. However, my desired result is not coming out. Now I need some suggestions with the actual code. Thanks!
Ineedhelpplz -7 Junior Poster in Training
I receive the following error when I compile my code:
blade71(154)% gcc -o vt visitype.c
Undefined first referenced
symbol in file
aVal /var/tmp//ccrbSM18.o
ld: fatal: Symbol referencing errors. No output written to vt
collect2: ld returned 1 exit status
blade71(155)% cat visitype.c
#include <stdio.h>
#define MAX 1000
void aVal(char asciiname[], char S[]);
main ()
{
int i, c, j;
char asciiname[] =
"NUL\0" "SOH\0" "STX\0" "ETX\0" "EOT\0" "ENQ\0" "ACK\0" "BEL\0"
" BS\0" " HT\0" " NL\0" " VT\0" " NP\0" " CR\0" " SO\0" " SI\0"
"DLE\0" "DC1\0" "DC2\0" "DC3\0" "DC4\0" "NAK\0" "SYN\0" "ETB\0"
"CAN\0" " EM\0" "SUB\0" "ESC\0" " FS\0" " GS\0" " RS\0" " VS\0"
" SP\0" " !\0" " \"\0" " #\0" " $\0" " %\0" " &\0" " '\0"
" (\0" " )\0" " *\0" " +\0" " ,\0" " -\0" " .\0" " /\0"
" 0\0" " 1\0" " 2\0" " 3\0" " 4\0" " 5\0" " 6\0" " 7\0"
" 8\0" " 9\0" " :\0" " ;\0" " <\0" " =\0" " >\0" " ?\0"
" @\0" " A\0" " B\0" " C\0" " D\0" " E\0" " F\0" " G\0"
" H\0" " I\0" " J\0" " K\0" " L\0" " M\0" " N\0" " O\0"
" P\0" " Q\0" " R\0" " S\0" " T\0" " U\0" " V\0" " W\0"
" X\0" " Y\0" " Z\0" " [\0" " \\\0" " ]\0" " ^\0" " _\0"
" `\0" " a\0" " b\0" " c\0" " d\0" " e\0" " f\0" …
Ineedhelpplz -7 Junior Poster in Training
blade71(43)% gcc -o reverse reverse_2.c
blade71(44)% reverse<reverse.in>reverse.out
blade71(45)% cat reverse.out
".doc no teid I .ssentaf a stneverp reven tsaf A !tnessid I :eton ,coD":teid no ,esruoc fo dnA
".ablE was I ere I saw elbA"
:taefed tsrif sih retfa ytilibapac sih no noelopaN
".madA m'I ,madaM"
:gniteem no ,nedE fo nedrag eht nI
?semordnilap fo draeh uoy evaHblade71(46)% od -x reverse_2.c
0000000 2369 6e63 6c75 6465 3c73 7464 696f 2e68
0000020 3e0a 2369 6e63 6c75 6465 203c 7374 646c
0000040 6962 2e68 3e0a 0a23 6465 6669 6e65 204d
0000060 4158 4c49 4e45 2031 3030 3030 0a76 6f69
0000100 6420 7265 7665 7273 6528 6368 6172 202a
0000120 7374 7229 3b0a 696e 7420 6d61 696e 2829
0000140 0a7b 0a09 696e 7420 632c 2069 3b0a 0963
0000160 6861 7220 6a3b 200a 0963 6861 7220 535b
0000200 4d41 584c 494e 455d 2c20 2a70 313b 0a09
0000220 7031 3d53 3b09 0a09 6320 3d20 6765 7463
0000240 6861 7228 293b 0a09 666f 7228 693d 303b
0000260 2063 213d 454f 463b 2063 3d67 6574 6368
0000300 6172 2829 290a 097b 0a09 535b 695d 3d20
0000320 633b 0a09 692b 2b3b 0a09 7d0a 0953 5b69
0000340 5d20 3d20 275c 3027 3b0a 0972 6576 6572
0000360 7365 2870 3129 3b0a 7265 7475 726e 2030
0000400 3b0a 7d0a 0a76 6f69 6420 7265 7665 7273
0000420 6528 6368 6172 202a 7374 7229 0a7b 0a09
0000440 …
Ineedhelpplz -7 Junior Poster in Training
In the reverse() function, there is a test for
if(*str)
. That means the function is expectingstr
to be a string. Strings in C are sequences of characters where the last character is a sentinel with the value of 0. This value is not added automatically, you have to insert it because you are manually building a string:S[i] = '\0'; reverse(p1);
so shouldn't the assignment read S[i-1]='\0'? Otherwise by putting the assignment you gave me in the body of my for loop every element of array S will be initialized to NULL?
Ineedhelpplz -7 Junior Poster in Training
Your code has three immediate problems:
- The i variable is not initialized to 0.
- getchar() is only called once, not every time the loop iterates.
- The array is not terminated with a '\0' character.
If you fix those things, it should work.
I have initialized the variable and fixed the problem with getchar. I don't quite understand your third advice however. Could you elaborate more please?
Ineedhelpplz -7 Junior Poster in Training
#include<stdio.h>
#include <stdlib.h>
#define MAXLINE 10000
void reverse(char *str);
int main()
{
int c, i;
char j;
char S[MAXLINE], *p1;
p1=S;
for(c=getchar(); c!=EOF; i++)
{
S[i]=c;
}
reverse(p1);
return 0;
}
void reverse(char *str)
{
if(*str)
{
reverse(str+1);
putchar(*str);
}
}
Please help me correct the segmentation fault.
blade71(107)% gcc -o reverse reverse_2.c
blade71(108)% reverse<reverse.in>reverse.out
Segmentation fault (core dumped)
blade71(109)%
Cat of files and octal dumps in hex:
Have you heard of palindromes?
In the garden of Eden, on meeting:
"Madam, I'm Adam."
Napoleon on his capability after his first defeat:
"Able was I ere I saw Elba."
And of course, on diet:
"Doc, note: I dissent! A fast never prevents a fatness. I diet on cod."
blade71(114)% od -x reverse.in
0000000 4861 7665 2079 6f75 2068 6561 7264 206f
0000020 6620 7061 6c69 6e64 726f 6d65 733f 0a0a
0000040 496e 2074 6865 2067 6172 6465 6e20 6f66
0000060 2045 6465 6e2c 206f 6e20 6d65 6574 696e
0000100 673a 0a0a 224d 6164 616d 2c20 4927 6d20
0000120 4164 616d 2e22 0a0a 4e61 706f 6c65 6f6e
0000140 206f 6e20 6869 7320 6361 7061 6269 6c69
0000160 7479 2061 6674 6572 2068 6973 2066 6972
0000200 7374 2064 6566 6561 743a 0a0a 2241 626c
0000220 6520 7761 7320 4920 6572 6520 4920 7361
0000240 7720 456c 6261 2e22 0a0a 416e 6420 6f66
0000260 2063 6f75 7273 652c 206f 6e20 6469 6574
…
Ineedhelpplz -7 Junior Poster in Training
Solved and completely re-done:
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 1000
/* This program will remove all trailing white space from an input file.
Using a loop containing several if statements to check for the ASCII
value of any white space generating characters; the program will
individually access each character until it reaches the EOF. */
int read(int index)
{
if (index < MAXLINE - 1)
return index + 1;
else
return 0;
}
int main(void)
{
char S[MAXLINE];
int head, tail, White_Space, Return_Value, c;
Return_Value = White_Space = head = tail = 0;
while ((c = getchar()) != EOF)
{
if (c == '\n')
{
head = tail = 0;
if (White_Space)
putchar('\n');
White_Space = 0;
}
else if (c == ' ' || c == '\t')
{
if (read(head) == tail)
{
putchar(S[tail]);
tail = read(tail);
White_Space = 1;
Return_Value = EXIT_FAILURE;
}
S[head] = c;
head = read(head);
}
else
{
while (head != tail)
{
putchar(S[tail]);
tail = read(tail);
}
putchar(c);
White_Space = 1;
}
}
return Return_Value;
}
Ineedhelpplz -7 Junior Poster in Training
still is infinitely looping though
Ineedhelpplz -7 Junior Poster in Training
while ((len = getline(line, MAXLINE))>0) { WS++; } while(WS>1)
What is the value of WS here? I mean, before you do the increment?
it should be set to 0 before manipulation of any data. Thank you
Ineedhelpplz -7 Junior Poster in Training
Instructions: Trailing blanks and tabs are defined as blanks and tabs that precede a '\n' character or an EOF to terminate a line, without any intervening characters of other kinds. You should copy a getline() function of the kind you see in the text to read in lines from stdin into an array, trim the line, and then write out the trimmed line to stdout, continuing until you encounter an EOF.
Note: If there are 4 non-blank lines in the input to trim, there should be 4 lines in the output, but lines consisting of only blanks, and tabs, should NOT be output. The trim function should not affect any blanks or tabs that do not come at the end of the line.
I have a runtime error. I octal dumped the input file and used the input file with the code I wrote to produce an output file and then octal dumped it. However, my octal dump of my out file is completely wrong! Somewhere in my program there is a non terminating loop; because I had to control C.
Here is the execution:
blade71(136)% gcc trim_3.c
blade71(137)% od -x trim.in
0000000 0909 4e6f 7720 6973 2074 6865 2020 2020
0000020 2020 200a 0909 5469 6d65 2066 6f72 0909
0000040 0a09 0961 6c6c 2067 6f6f 6420 6d65 6e20
0000060 746f 2020 2020 0a09 0963 6f6d 6520 746f
0000100 2074 6865 2061 6964 206f 6620 7468 6520
0000120 …
Ineedhelpplz -7 Junior Poster in Training
I am going to mark this thread as solved and start a new one called "Trim.c" as I just realized this thread is de-railed and has nothing to do with gcc and getline anymore. Thank you all for your help so far.
Ineedhelpplz -7 Junior Poster in Training
Fixed the problem however I now have a runtime error. I octal dumped the input file and used the input file with the code I wrote to produce an output file and then octal dumped it. However, my octal dump of my out file is completely wrong! Somewhere in my program there is a non terminating loop; because I had to control C.
Here is the execution:
blade71(136)% gcc trim_3.c
blade71(137)% od -x trim.in
0000000 0909 4e6f 7720 6973 2074 6865 2020 2020
0000020 2020 200a 0909 5469 6d65 2066 6f72 0909
0000040 0a09 0961 6c6c 2067 6f6f 6420 6d65 6e20
0000060 746f 2020 2020 0a09 0963 6f6d 6520 746f
0000100 2074 6865 2061 6964 206f 6620 7468 6520
0000120 7061 7274 792e 2020 2020 2020 0a0a
0000136
blade71(138)% gcc -o trim trim_3.c
blade71(144)% dir
In_Files core trim trim.c trim.in trim_2.c trim_3.c
blade71(145)% trim<trim.in>trim.out
^C
blade71(146)% od -x trim.out
0000000 ffff ffff ffff ffff ffff ffff ffff ffff
*
12260000
Here is the final source:
blade71(171)% cat trim_3.c
#include<stdio.h>
#define MAXLINE 1000
int getline (char line[], int maxline);
int copy_NW(char to[], char from [], int NWS);
main ()
{
int NL, WS, len;
char line[MAXLINE];
char line_NW[MAXLINE];
while ((len = getline(line, MAXLINE))>0)
{
WS++;
}
while(WS>1)
{
copy_NW(line, line_NW, WS);
}
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i …
Ineedhelpplz -7 Junior Poster in Training
New and updated code with errors of course....this program is just not my program. The following errors on compilation and I really am not too sure why. Could someone please explain?
]blade71(115)% gcc trim_3.c
trim_3.c: In function `copy_NW':
trim_3.c:43: error: invalid lvalue in assignment
blade71(119)% cat trim_3.c
#include<stdio.h>
#define MAXLINE 1000
int getline (char line[], int maxline);
int copy_NW(char to[], char from [], int NWS);
main ()
{
int NL, WS, len;
char line[MAXLINE];
char line_NW[MAXLINE];
while ((len = getline(line, MAXLINE))>0)
{
WS++;
}
while(WS>1)
{
copy_NW(line, line_NW, WS);
}
return 0;
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i<lim -1 && (c=getchar())!= EOF && c!='\n'; ++i)
s[i]= c;
if (c=='\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int copy_NW(char line[], char line_NW[], int NL)
{
int c, d;
int i = 0;
while(NL != 0 && c=getchar()!=EOF)
line[i] = line_NW[i];
if(c==' ' || c=='\n' || c=='\t')
NL--;
d=putchar(c);
line_NW[i] = d;
return d;
}
Ineedhelpplz -7 Junior Poster in Training
I think one of the issues here is that the OP has not been very clear as to the requirements/expectations of the program.
Because I do not want someone to go oooo here it is: "post random code". Aside from all this I am posting the updated code which now executes, yet when I compile it using a.out<trim.in>trim.out my trim.out file is all blank. Clearly I am not using putchar right to write to trim.out. I think the main issue is with my second while loop never terminating. The next update of the code will contain arrays as already stated to store each individual element, then I will read through the array containing original characters and copy it to a second array. Removing any whitespace characters that it encounters while for looping through the copy....I have not yet figured out how to allocate the size of the second array though. After I get the code correctly working, I plan to break it up into more individual functions so the code is essentially just a main calling different functions to do all the work. I feel that I might have to make it like this and use gdb to find where the error is that has my trim.out consisting of nothing more than '0'. The original code has been scrapped for this completely new approach
For Kermit:
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] …
Ineedhelpplz -7 Junior Poster in Training
I actually found the problem....google "getline solaris". Because I am on a Sunblade(Solaris) I cannot use getln, fgets, or basically anything other than string.h which is not supposed to be used for this program. My next way of attempting to simply solve the program will be to copy every individual character into an array using getchar and then begin copying the array into a second array that has an if statement that checks if it is a whitespace character duplicate before copying. Can anyone think of any better way to do this that does not use any external libraries or getline, getln, fgets?....basically just getchar() and putchar()
Ineedhelpplz -7 Junior Poster in Training
This is the error I have been receiving when trying to compile my code:
blade71(45)% gcc -o trim trim.c
Undefined first referenced
symbol in file
getline /var/tmp//ccYdyoM3.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
My source is only a rough draft (Done in roughly 20 minutes). I have not yet began to break up the code into functions although I plan on line counting, whitespace counting, and trimming the whitespace all in separate functions. The problem is from K&R Programming C. The program is supposed to remove all excess whitespace from a program so when you do an octal dump there are no excess whitespace characters. Please don't tell me to use a library outside of stdio.h or how to do my program unless you see something dramatically wrong. (aside from my lack of functions and java like syntax)
#include <stdio.h>
/* This program will remove all trailing white space from an input file.
Using a loop containing several if statements to check for the ASCII
value of any white space generating characters; the program will
individually access each character until it reaches the EOF. */
main()
{
int c, newline, line, bytes_read;
int trim_count;
int nbytes = 100;
char *my_string;
my_string = (char *) malloc (nbytes + 1);
bytes_read = getline (&my_string, nbytes, stdin);
while ((c = getchar())!= EOF)
{
if (c == '\n' || c == ' ' || c == '\t') …
Ineedhelpplz -7 Junior Poster in Training
all the errors have been resolved. The correct printf syntax has also been implemented. Now however, I am having a problem getting the mid value. When my code gets to the printing mid section it just prints a blank line for mid...displaying nothing in otherwords. Why is this? I have defined mid as (low + high) / 2 where low = 0 and high = n. So if 10 is entered the mid should be 5. Then when compiled with gcc as the linker between the .c file and .asm it gets to that blank mid line and then terminates due to a floating point exception. However, I use integers and no floating point numbers or IEEE in my assembly code. Therefore, it is impossible to have a floating point exception when using doublewords which are made up of integers and ints. I could see if I was using 64 floating point bit instructions in the .asm file and %lf and declaring my variables as floats running into a floating point exception. But nowhere in either of the linked sources is there any use of floating point numbers. The only thing I can figure is when the assembly program attempts to use its search function something is going terribly wrong.
In conclusion, I would like to know if there is something wrong with my C code or my assembly code....As I see nothing wrong with either.
The new updated C source:
#include<stdio.h>
main()
{
int a[10], low=0, …
Ineedhelpplz -7 Junior Poster in Training
I think the conversation is loosing track. You want to write a C program to write main function,take input in array and then sort it through some sorting algorithm and then run search through your assembly program right? Concentrate on that.Do this first:
1)Writing a main program and taking inputs into array should be simple as it is very much similar to JAVA programming the same thing.Do it first.
2)You will get all the help needed for sorting algorithms form this community threads and also the link
http://en.wikipedia.org/wiki/Sorting_algorithm3)If you want it still simplified here you get algorithm and modules to write bubble sort and also simulated explaination.
http://www.cs.princeton.edu/~ah/alg_anim/gawain-4.0/BubbleSort.html4)After this concentrate on making it search for the user input in the array through your assembly code.
Don't loose your way.Concentrate on what path to take to make compiler do your work.Then you can code anything.
K I took what someone else gave me and played around with it for a while and its still not working. How about I write what im supposed to do in java to prove that im not an idiot and just not used to C syntax and then someone translates that into C for me? Just kidding, but seriously this is aggrivating. My deadline is arriving and I am still recieving the following error messages:
HW7C.c: In function `main':
HW7C.c:24: error: syntax error before "value"
HW7C.c:24: error: syntax error before ')' token
The source:
#include<stdio.h>
main()
{
int …
Ineedhelpplz -7 Junior Poster in Training
bump
MosaicFuneral commented: Â +0
Narue commented: Bumping is rude, kthxbye. -7
Ineedhelpplz -7 Junior Poster in Training
if its jus that then below would be your main program
#include<stdio.h>
void main()
{
int a[10], low=0,high=0,mid=0,value,n,i;
printf("enter the no of elements needed");
scanf("%d",&n);
printf("enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&a);
printf("enter value to be searched ");
scanf("%d"&value);
low = 0;
high = n;
mid = (low + high ) /2;
for(i=0;i<n;i++)
{
k=search(a,low, high,value);
}if(k== -1)
printf("element not found");
else printf("search succesfful, the value %d is found at %d,value,mid+1);
}
i hpe it works..
Compiled using GCC linked with your code:
HW7C.c: In function `main':
HW7C.c:12: error: invalid operands to binary &
HW7C.c:18: error: `k' undeclared (first use in this function)
HW7C.c:18: error: (Each undeclared identifier is reported only once
HW7C.c:18: error: for each function it appears in.)
HW7C.c:23: error: missing terminating " character
HW7C.c:24: error: syntax error before '}' token
HW7C.c:4: warning: return type of 'main' is not `int'
Ineedhelpplz -7 Junior Poster in Training
Hello,
I have been given an assignment in which I am to write an assembly language version of the following binary search function & use a suitable main program written in C to test my function. The function in C in which I had to translate to assembly is:
int search(int a[], int low, int high, int value)
{
int mid;
if (low > high)
return -1;
mid = ( low + high) / 2;
if (a[mid] == value)
return mid;
else if (value < a[mid])
return search(a, low, mid - 1, value);
else
return search(a, mid + 1, high, value);
}
The assembly code which I wrote (Yes, I was hungry):
[SECTION .text]
global search
search: push EBP
mov EBP, ESP
add ESP, -4 ; Allocate mid at EBP - 4
lea EBX, [EBP + 4] ; Load "value" into EBX
lea ECX, [EBP + 8] ; Load "high" into ECX
lea EDI, [EBP + 12] ; Load "Low" into EDI
lea ESI, [EBP + 16] ; Load array *P into ESI
cmp EDI, ECX ; if (low>high)
jg poptarts ; jmp to return -1
jmp fishsticks ; Keep going
poptarts:
mov EAX, -1 ; return -1
jmp last
fishsticks:
; mov [EBP - 4], 0 ; Mid = 0
mov [EBP - 4], EDI ; Mid = low
add [EBP - 4], ECX ; Mid = low + high
mov EAX, [EBP - 4] ; EAX = Mid == (low + high)
mov EDX, 2 …
Ineedhelpplz -7 Junior Poster in Training
disregard the line push EDI, it has been removed and I forgot to remove it before posting. Also, do I even need to have fmt1? It seems like I am not implementing it at all. I literally just learned arrays last week and now my professor randomly decided to move onto implimentation of C functions (When I don't know C well, I took courses in java....therefore, I didn't even know what a pointer was until 2 days ago.).
Ineedhelpplz -7 Junior Poster in Training
Ok, so I completely fixed my code taking your advice to the best of my ability. Removing alot of unecessary push commands. My program now works, however when exiting the next1 label and proceeding to step1 my values are lost. I believe this is because printf and scanf are known to mess with values in EAX, EBX, ECX, and EDX. If someone could please tell me if this is the case, I can just put the *pSum in EDI or ESI instead of ECX. If this isn't the case, could someone please point out what I am doing wrong. My main works, my function works, but transfering data from the function back into the main is a catastrophe.
Thank You,
Code:
[SECTION .data]
sum: dd 0
n: dd 0
fmt1: db "%d", 0
[SECTION .text]
global main
extern scanf, printf
main: push EBP
mov EBP, ESP
push EBX
push ESI
push EDI
first:
push dword sum
push dword n
push fmt1
call scanf
add ESP, 12
cool:
mov EDI, [n]
push EDI
call sumPowers
add ESP, 4
call printf
add ESP, 4
last: pop EDI
pop ESI
pop EBX
mov ESP, EBP
pop EBP
ret
sumPowers:
push EBP
mov ESP,EBP
mov ECX, 2
mov EBX, 2
mov EDX,3
mov EAX, 0 ; Initialize the counter
mov ESI, EDI ; Store n in ESI
sub ESI,1
loop1: cmp EAX, ESI
jge next
imul ECX,EBX
add EDX,ECX
add EAX,1
jmp loop1
next:
mov ECX, EDX
mov ECX, [EBP + 12] …
Ineedhelpplz -7 Junior Poster in Training
So, I talked to my professor and apparently all I am doing is taking the value in EDI, adding w/e to EBP until I get to the pointer address (sum) in the stack. Then I move the value in EDI into the pointer address.