SlideShare a Scribd company logo
1 of 11
Download to read offline
I am having problems getting the correct output. Here are the instructions:
For this assignment, you will write a program that will accept up to 2048 characters from stdin
and emit a report to stdout listing each unique word found in the input, the offset of the
beginning of the word from the beginning of the input, the number of times the word was in the
input and the word itself. You should store the input characters into a character array as your
working buffer.
A word is defined as any sequence of non-whitespace characters.
The input will terminate with an EOF if there are less than 2048 characters, but your program
shall accept no more than 2048.
Your program shall record the beginning and end of each word in the buffer and use the buffer
itself as the storage for the content of each word. The report needs to be generated from the
buffer
and the index to the words in the buffer.
You are not allowed to declare or use any global variables. All variables must be locally scoped.
You may however add any additional #define directives you deem needed or helpful.
Here is my code so far:
.......................................................................................................................
#include <stdio.h>
#include <ctype.h>
#include "string_index.h"
int main(void) {
// variable declartaions
char buffer[BUFFER_SIZE]; // begins 2048 buffer
int word_index_array[WORD_LIMIT][2]; // index by array
int word_counts[WORD_LIMIT]; // number of occurances
const char *word_index[WORD_LIMIT][2]; // index by pointer
int words_found; // shows the words found
// initialize storage
for (int i = 0; i < BUFFER_SIZE; i++) {
buffer[i] = '0';
}
/* ***** DO NOT CHANGE ANY CODE BEYOND THIS POINT IN main() ****** */
initialize_buffer(buffer);
words_found = index_buffer_by_ptr(buffer, word_index, word_counts);
print_report_by_ptr(buffer, word_index, word_counts, words_found);
words_found = index_buffer_by_array(buffer, word_index_array, word_counts);
print_report_by_array(buffer, word_index_array, word_counts, words_found);
} // end main
/* ***** YOUR FUNNCTION DEFINTIONS BELOW ***** */
int initialize_buffer(char buffer[]) {
int i = 0;
int c;
while (i < BUFFER_SIZE - 1 && (c = getchar()) != EOF) {
buffer[i++] = c;
}
buffer[i] = '0';
if (i == 0) {
return -1;
} else {
return i;
}
};
int index_buffer_by_array(const char buffer[], int index[][2], int w_counts[] ) {
int word_cnt = 0;
int len = strlen(buffer);
int i = 0;
int word_beg = 0;
int word_end = 0;
int is_word = 0;
for (i = 0; i < len; i++) {
if (!is_word && !isspace(buffer[i])) {
word_beg = i;
is_word = 1;
} else if (is_word && isspace(buffer[i])) {
word_end = i - 1;
is_word = 0;
int j;
for (j = 0; j < word_cnt; j++) {
if (word_end - word_beg != index[j][1] - index[j][0]) {
continue;
}
int k;
for (k = index[j][0]; k <= index[j][1]; k++) {
if (buffer[word_beg + k - index[j][0]] != buffer[k]) {
break;
}
}
if (k == index[j][1] + 1) {
w_counts[j]++;
break;
}
}
if (j == word_cnt) {
index[j][0] = word_beg;
index[j][1] = word_end;
w_counts[word_cnt] = 1;
word_cnt++;
}
}
}
return word_cnt;
};
int find_word_by_array(int word_beg, const char buf[], int index[][2]) {
for (int i = 0; index[i][WD_BGN] != -1; i++) {
if (strncmp(&buf[word_beg], &buf[index[i][WD_BGN]], index[i][WD_END] -
index[i][WD_BGN] + 1) == 0) {
return i;
}
}
return NOT_FOUND;
};
void print_report_by_array(const char buf[], int index[][2], int counts[], int word_cnt) {
for (int i = 0; i < word_cnt; i++) {
printf("%d(%2d): %d %.*sn", i, index[i][WD_BGN], counts[i], index[i][WD_END] -
index[i][WD_BGN] + 1, &buf[index[i][WD_BGN]]);
}
};
int index_buffer_by_ptr(const char * buf, const char * index[][2], int word_counts[] ) {
int i = 0;
int w_count = 0;
while (*buf != '0') {
if (!isspace(*buf)) {
// Found a word
const char *word_beg = buf;
while (!isspace(*buf) && *buf != '0') {
buf++;
}
const char *word_end = buf - 1;
int index_found = find_word_by_ptr(word_beg, index);
if (index_found == NOT_FOUND) {
// New word found
index[w_count][WD_BGN] = word_beg;
index[w_count][WD_END] = word_end;
word_counts[w_count] = 1;
w_count++;
} else {
// Existing word found
word_counts[index_found]++;
}
} else {
buf++;
}
}
return w_count;
};
int find_word_by_ptr(const char *beg, const char *index[][2]) {
int i, j;
for (i = 0; index[i][WD_END] != NULL; i++) {
if (index[i][WD_BGN] == NULL) {
continue;
}
if (strlen(beg) != index[i][WD_END] - index[i][WD_BGN]) {
continue;
}
for (j = 0; j < index[i][WD_END] - index[i][WD_BGN]; j++) {
if (*(beg + j) != *(index[i][WD_BGN] + j)) {
break;
}
}
if (j == index[i][WD_END] - index[i][WD_BGN]) {
return i;
}
}
return NOT_FOUND;
};
void print_report_by_ptr(const char *buf, const char *index[][2], int counts[], int word_cnt) {
for (int i = 0; i < word_cnt; i++) {
printf("%d(%2d): %d %.*sn", i, (int)(index[i][WD_BGN] - buf), counts[i],
(int)(index[i][WD_END] - index[i][WD_BGN] + 1), index[i][WD_BGN]);
}
};
.........................................................................................................................
Ande here is the header file:
..........................................................................................................................
#include <stdio.h>
#include <ctype.h>
#ifndef STRING_INDEX_HEADER
#define STRING_INDEX_HEADER
#define BUFFER_SIZE 2048
#define WORD_LIMIT 100
#define NOT_FOUND -1
#define WD_BGN 0
#define WD_END 1
// Prototypes
/*
initialize_buffer:
Fills the given array with characters from stdin until either EOF or
2048 characters have been loaded.
Returns the total number of characters loaded and -1 if no characters loaded.
*/
int initialize_buffer(char buffer[]);
/********************************************************
Functions that work with arrays
*******************************************************
*/
/*
index_buffer_by_array:
Indexes the beginning and end of each unique word (string of non-whitespace characters).
The index is a list of beginning and ending array indexes for each unique word found.
Duplicate words are counted in w_counts array.
Returns the total number of unique words found in the character buffer.
*/
int index_buffer_by_array(const char buffer[], int index[][2], int w_counts[] );
/*
find_word_by_array:
Determines if the word in buf[] beginning at index word_beg is already indexed.
A word terminates with the first whitespace character.
Returns the index number for the word if found, otherwise returns NOT_FOUND.
*/
int find_word_by_array(int word_beg, const char buf[], int index[][2]);
/*
print_report_by_array:
Prints to stdout a report giving the word index number, offset of first letter from
beginning of the buffer, the word count, and the word itself. This function expects index
to be an array of array indexes for the beginning and end of each word.
Example output for buffer containing the characters in quotes "all the all"
0( 0): 2 all
1( 4): 1 the
*/
void print_report_by_array(const char buf[], int index[][2], int counts[], int word_cnt);
/*******************************************************
Functions that work with pointers
********************************************************/
/*
index_buffer_by_ptr:
Indexes the beginning and end of each unique word (string of non-whitespace characters).
The index is a list of beginning and ending char pointers for each unique word found.
Duplicate words are counted in w_counts array.
Returns the total number of unique words found in the character buffer.
*/
int index_buffer_by_ptr(const char * buf, const char * index[][2], int word_counts[] );
/*
find_word_by_ptr:
Determines if the word in beginning at the char * beg is already indexed.
A word terminates with the first whitespace character.
Returns the index number for the word if found, otherwise returns NOT_FOUND.
*/
int find_word_by_ptr(const char * beg, const char * index[][2]);
/*
print_report_by_ptr:
Prints to stdout a report giving the word index number, offset of first letter from
beginning of the buffer, the word count, and the word itself. This function expects index
to be an array of char pointers for the beginning and end of each word.
Example output for buffer containing the characters in quotes "all the all"
0( 0): 2 all
1( 4): 1 the
*/
void print_report_by_ptr(const char * buf, const char * index[][2], int counts[], int word_cnt);
#endif
...........................................................................................................................
Here is my current output (NOT WHAT I AM LOOKING FOR):
0( 0): 1 How
1( 4): 1 many
2( 9): 1 wood
3(14): 1 chucks
4(21): 1 would
5(27): 1 chuck
6(33): 1 wood
7(38): 1 if
8(41): 1 a
9(43): 1 wood
10(48): 1 chuck
11(54): 1 could
12(60): 1 chuck
13(66): 1 wood.
0( 0): 1 How
1( 4): 1 many
2( 9): 3 wood
3(14): 1 chucks
4(21): 1 would
5(27): 3 chuck
6(38): 1 if
7(41): 1 a
8(54): 1 could
My array functions almost works, it is just cutting off the last word of the input and it counts
words with an uppercase letter as different than a lowercase (e.g., 0(0) 1 All, 1(4) 1 all).
Everything else about that is correct. My ptr functions are not working correctly. I am trying to
show each unique word once with the count next to them. We are not using c string in this so
don't use 'strcmp' please.

More Related Content

Similar to I am having problems getting the correct output- Here are the instruct (1).pdf

C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
c program presentation on format specifer.pptx
c program presentation on format  specifer.pptxc program presentation on format  specifer.pptx
c program presentation on format specifer.pptxPuskar Bhandari
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanMohammadSalman129
 
So basically I worked really hard on this code in my CS150 class and.pdf
So basically I worked really hard on this code in my CS150 class and.pdfSo basically I worked really hard on this code in my CS150 class and.pdf
So basically I worked really hard on this code in my CS150 class and.pdfeyewaregallery
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxRamakrishna Reddy Bijjam
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxDIPESH30
 
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdfC++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdfrahulfancycorner21
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in Crgnikate
 
I need help building a dictionary for the unique packets tha.pdf
I need help building a dictionary for the unique packets tha.pdfI need help building a dictionary for the unique packets tha.pdf
I need help building a dictionary for the unique packets tha.pdfsukhvir71
 
Write a program that takes any input text and produces both a frequen.pdf
Write a program that takes any input text and produces both a frequen.pdfWrite a program that takes any input text and produces both a frequen.pdf
Write a program that takes any input text and produces both a frequen.pdfarenamobiles123
 
Hello, I need help with the following assignmentThis assignment w.pdf
Hello, I need help with the following assignmentThis assignment w.pdfHello, I need help with the following assignmentThis assignment w.pdf
Hello, I need help with the following assignmentThis assignment w.pdfnamarta88
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingKevlin Henney
 

Similar to I am having problems getting the correct output- Here are the instruct (1).pdf (20)

C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
7512635.ppt
7512635.ppt7512635.ppt
7512635.ppt
 
Ponters
PontersPonters
Ponters
 
c program presentation on format specifer.pptx
c program presentation on format  specifer.pptxc program presentation on format  specifer.pptx
c program presentation on format specifer.pptx
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
C programming
C programmingC programming
C programming
 
So basically I worked really hard on this code in my CS150 class and.pdf
So basically I worked really hard on this code in my CS150 class and.pdfSo basically I worked really hard on this code in my CS150 class and.pdf
So basically I worked really hard on this code in my CS150 class and.pdf
 
See through C
See through CSee through C
See through C
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docxLab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
Lab01Filesbuild.bat@echo offclsset DRIVE_LETTER=1.docx
 
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdfC++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
 
Types of pointer
Types of pointerTypes of pointer
Types of pointer
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
I need help building a dictionary for the unique packets tha.pdf
I need help building a dictionary for the unique packets tha.pdfI need help building a dictionary for the unique packets tha.pdf
I need help building a dictionary for the unique packets tha.pdf
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Write a program that takes any input text and produces both a frequen.pdf
Write a program that takes any input text and produces both a frequen.pdfWrite a program that takes any input text and produces both a frequen.pdf
Write a program that takes any input text and produces both a frequen.pdf
 
Hello, I need help with the following assignmentThis assignment w.pdf
Hello, I need help with the following assignmentThis assignment w.pdfHello, I need help with the following assignmentThis assignment w.pdf
Hello, I need help with the following assignmentThis assignment w.pdf
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
 

More from sonunotwani

I arncinn curfaca All of the units are sedimentary rocks- K and J are.pdf
I arncinn curfaca All of the units are sedimentary rocks- K and J are.pdfI arncinn curfaca All of the units are sedimentary rocks- K and J are.pdf
I arncinn curfaca All of the units are sedimentary rocks- K and J are.pdfsonunotwani
 
i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfsonunotwani
 
Hydrogen is the lightest element- If two hydrogen atoms are near each.pdf
Hydrogen is the lightest element- If two hydrogen atoms are near each.pdfHydrogen is the lightest element- If two hydrogen atoms are near each.pdf
Hydrogen is the lightest element- If two hydrogen atoms are near each.pdfsonunotwani
 
Hydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdf
Hydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdfHydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdf
Hydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdfsonunotwani
 
How would I be able to add in two columns to this dataset- 1 with the (1).pdf
How would I be able to add in two columns to this dataset- 1 with the (1).pdfHow would I be able to add in two columns to this dataset- 1 with the (1).pdf
How would I be able to add in two columns to this dataset- 1 with the (1).pdfsonunotwani
 
I am in need of an example of how to do it using the GUI Design and im.pdf
I am in need of an example of how to do it using the GUI Design and im.pdfI am in need of an example of how to do it using the GUI Design and im.pdf
I am in need of an example of how to do it using the GUI Design and im.pdfsonunotwani
 
Hyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdf
Hyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdfHyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdf
Hyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdfsonunotwani
 
Huang Ltd- records their business transactions into the basic accounti.pdf
Huang Ltd- records their business transactions into the basic accounti.pdfHuang Ltd- records their business transactions into the basic accounti.pdf
Huang Ltd- records their business transactions into the basic accounti.pdfsonunotwani
 
How would I change my code to get the picture below- i.pdf
How would I change my code to get the picture below-    i.pdfHow would I change my code to get the picture below-    i.pdf
How would I change my code to get the picture below- i.pdfsonunotwani
 
How would payments to acquire debt instruments of other entities be cl.pdf
How would payments to acquire debt instruments of other entities be cl.pdfHow would payments to acquire debt instruments of other entities be cl.pdf
How would payments to acquire debt instruments of other entities be cl.pdfsonunotwani
 
How would you determine the relative ages of igneous rocks- A- Cross-c.pdf
How would you determine the relative ages of igneous rocks- A- Cross-c.pdfHow would you determine the relative ages of igneous rocks- A- Cross-c.pdf
How would you determine the relative ages of igneous rocks- A- Cross-c.pdfsonunotwani
 
How would you answer these questions- 1- How would you feel if a job.pdf
How would you answer these questions-  1- How would you feel if a job.pdfHow would you answer these questions-  1- How would you feel if a job.pdf
How would you answer these questions- 1- How would you feel if a job.pdfsonunotwani
 
How would you describe JD's product-.pdf
How would you describe JD's product-.pdfHow would you describe JD's product-.pdf
How would you describe JD's product-.pdfsonunotwani
 
How would the central fight against a recessionary gap- Describe the r.pdf
How would the central fight against a recessionary gap- Describe the r.pdfHow would the central fight against a recessionary gap- Describe the r.pdf
How would the central fight against a recessionary gap- Describe the r.pdfsonunotwani
 
How would E-coli respond to the environmental change if it has been pl.pdf
How would E-coli respond to the environmental change if it has been pl.pdfHow would E-coli respond to the environmental change if it has been pl.pdf
How would E-coli respond to the environmental change if it has been pl.pdfsonunotwani
 
HS450-1- Assess strategic planning techniques for organizational chang.pdf
HS450-1- Assess strategic planning techniques for organizational chang.pdfHS450-1- Assess strategic planning techniques for organizational chang.pdf
HS450-1- Assess strategic planning techniques for organizational chang.pdfsonunotwani
 
Human activity has increased habitat fragmentation and restricted wild.pdf
Human activity has increased habitat fragmentation and restricted wild.pdfHuman activity has increased habitat fragmentation and restricted wild.pdf
Human activity has increased habitat fragmentation and restricted wild.pdfsonunotwani
 
Suppose that the feasible region of a maximization LP problem has corn.pdf
Suppose that the feasible region of a maximization LP problem has corn.pdfSuppose that the feasible region of a maximization LP problem has corn.pdf
Suppose that the feasible region of a maximization LP problem has corn.pdfsonunotwani
 
Suppose that it is the year 1999 and the U-S- government has a budget.pdf
Suppose that it is the year 1999 and the U-S- government has a budget.pdfSuppose that it is the year 1999 and the U-S- government has a budget.pdf
Suppose that it is the year 1999 and the U-S- government has a budget.pdfsonunotwani
 
Suppose that past history shows that 30- of university students are sm.pdf
Suppose that past history shows that 30- of university students are sm.pdfSuppose that past history shows that 30- of university students are sm.pdf
Suppose that past history shows that 30- of university students are sm.pdfsonunotwani
 

More from sonunotwani (20)

I arncinn curfaca All of the units are sedimentary rocks- K and J are.pdf
I arncinn curfaca All of the units are sedimentary rocks- K and J are.pdfI arncinn curfaca All of the units are sedimentary rocks- K and J are.pdf
I arncinn curfaca All of the units are sedimentary rocks- K and J are.pdf
 
i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
 
Hydrogen is the lightest element- If two hydrogen atoms are near each.pdf
Hydrogen is the lightest element- If two hydrogen atoms are near each.pdfHydrogen is the lightest element- If two hydrogen atoms are near each.pdf
Hydrogen is the lightest element- If two hydrogen atoms are near each.pdf
 
Hydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdf
Hydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdfHydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdf
Hydrogen bonds Helicase Topoisomerases Pseudoautosomal Regions Barr Bo.pdf
 
How would I be able to add in two columns to this dataset- 1 with the (1).pdf
How would I be able to add in two columns to this dataset- 1 with the (1).pdfHow would I be able to add in two columns to this dataset- 1 with the (1).pdf
How would I be able to add in two columns to this dataset- 1 with the (1).pdf
 
I am in need of an example of how to do it using the GUI Design and im.pdf
I am in need of an example of how to do it using the GUI Design and im.pdfI am in need of an example of how to do it using the GUI Design and im.pdf
I am in need of an example of how to do it using the GUI Design and im.pdf
 
Hyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdf
Hyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdfHyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdf
Hyper-IgM syndrome is a rare genetic disease caused by a loss-of-funct.pdf
 
Huang Ltd- records their business transactions into the basic accounti.pdf
Huang Ltd- records their business transactions into the basic accounti.pdfHuang Ltd- records their business transactions into the basic accounti.pdf
Huang Ltd- records their business transactions into the basic accounti.pdf
 
How would I change my code to get the picture below- i.pdf
How would I change my code to get the picture below-    i.pdfHow would I change my code to get the picture below-    i.pdf
How would I change my code to get the picture below- i.pdf
 
How would payments to acquire debt instruments of other entities be cl.pdf
How would payments to acquire debt instruments of other entities be cl.pdfHow would payments to acquire debt instruments of other entities be cl.pdf
How would payments to acquire debt instruments of other entities be cl.pdf
 
How would you determine the relative ages of igneous rocks- A- Cross-c.pdf
How would you determine the relative ages of igneous rocks- A- Cross-c.pdfHow would you determine the relative ages of igneous rocks- A- Cross-c.pdf
How would you determine the relative ages of igneous rocks- A- Cross-c.pdf
 
How would you answer these questions- 1- How would you feel if a job.pdf
How would you answer these questions-  1- How would you feel if a job.pdfHow would you answer these questions-  1- How would you feel if a job.pdf
How would you answer these questions- 1- How would you feel if a job.pdf
 
How would you describe JD's product-.pdf
How would you describe JD's product-.pdfHow would you describe JD's product-.pdf
How would you describe JD's product-.pdf
 
How would the central fight against a recessionary gap- Describe the r.pdf
How would the central fight against a recessionary gap- Describe the r.pdfHow would the central fight against a recessionary gap- Describe the r.pdf
How would the central fight against a recessionary gap- Describe the r.pdf
 
How would E-coli respond to the environmental change if it has been pl.pdf
How would E-coli respond to the environmental change if it has been pl.pdfHow would E-coli respond to the environmental change if it has been pl.pdf
How would E-coli respond to the environmental change if it has been pl.pdf
 
HS450-1- Assess strategic planning techniques for organizational chang.pdf
HS450-1- Assess strategic planning techniques for organizational chang.pdfHS450-1- Assess strategic planning techniques for organizational chang.pdf
HS450-1- Assess strategic planning techniques for organizational chang.pdf
 
Human activity has increased habitat fragmentation and restricted wild.pdf
Human activity has increased habitat fragmentation and restricted wild.pdfHuman activity has increased habitat fragmentation and restricted wild.pdf
Human activity has increased habitat fragmentation and restricted wild.pdf
 
Suppose that the feasible region of a maximization LP problem has corn.pdf
Suppose that the feasible region of a maximization LP problem has corn.pdfSuppose that the feasible region of a maximization LP problem has corn.pdf
Suppose that the feasible region of a maximization LP problem has corn.pdf
 
Suppose that it is the year 1999 and the U-S- government has a budget.pdf
Suppose that it is the year 1999 and the U-S- government has a budget.pdfSuppose that it is the year 1999 and the U-S- government has a budget.pdf
Suppose that it is the year 1999 and the U-S- government has a budget.pdf
 
Suppose that past history shows that 30- of university students are sm.pdf
Suppose that past history shows that 30- of university students are sm.pdfSuppose that past history shows that 30- of university students are sm.pdf
Suppose that past history shows that 30- of university students are sm.pdf
 

Recently uploaded

Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatmentsaipooja36
 
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...Sumit Tiwari
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryCeline George
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Celine George
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024CapitolTechU
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxheathfieldcps1
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading RoomImplanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading RoomSean M. Fox
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
Benefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptxBenefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptxsbabel
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...Nguyen Thanh Tu Collection
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
Poster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfPoster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfAlexander Litvinenko
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the lifeNitinDeodare
 

Recently uploaded (20)

Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
How to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 InventoryHow to Manage Closest Location in Odoo 17 Inventory
How to Manage Closest Location in Odoo 17 Inventory
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
Mattingly "AI and Prompt Design: LLMs with Text Classification and Open Source"
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading RoomImplanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
Implanted Devices - VP Shunts: EMGuidewire's Radiology Reading Room
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Benefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptxBenefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptx
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Poster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdfPoster_density_driven_with_fracture_MLMC.pdf
Poster_density_driven_with_fracture_MLMC.pdf
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 

I am having problems getting the correct output- Here are the instruct (1).pdf

  • 1. I am having problems getting the correct output. Here are the instructions: For this assignment, you will write a program that will accept up to 2048 characters from stdin and emit a report to stdout listing each unique word found in the input, the offset of the beginning of the word from the beginning of the input, the number of times the word was in the input and the word itself. You should store the input characters into a character array as your working buffer. A word is defined as any sequence of non-whitespace characters. The input will terminate with an EOF if there are less than 2048 characters, but your program shall accept no more than 2048. Your program shall record the beginning and end of each word in the buffer and use the buffer itself as the storage for the content of each word. The report needs to be generated from the buffer and the index to the words in the buffer. You are not allowed to declare or use any global variables. All variables must be locally scoped. You may however add any additional #define directives you deem needed or helpful. Here is my code so far: ....................................................................................................................... #include <stdio.h> #include <ctype.h> #include "string_index.h" int main(void) { // variable declartaions char buffer[BUFFER_SIZE]; // begins 2048 buffer int word_index_array[WORD_LIMIT][2]; // index by array int word_counts[WORD_LIMIT]; // number of occurances const char *word_index[WORD_LIMIT][2]; // index by pointer int words_found; // shows the words found
  • 2. // initialize storage for (int i = 0; i < BUFFER_SIZE; i++) { buffer[i] = '0'; } /* ***** DO NOT CHANGE ANY CODE BEYOND THIS POINT IN main() ****** */ initialize_buffer(buffer); words_found = index_buffer_by_ptr(buffer, word_index, word_counts); print_report_by_ptr(buffer, word_index, word_counts, words_found); words_found = index_buffer_by_array(buffer, word_index_array, word_counts); print_report_by_array(buffer, word_index_array, word_counts, words_found); } // end main /* ***** YOUR FUNNCTION DEFINTIONS BELOW ***** */ int initialize_buffer(char buffer[]) { int i = 0; int c; while (i < BUFFER_SIZE - 1 && (c = getchar()) != EOF) { buffer[i++] = c; } buffer[i] = '0'; if (i == 0) { return -1; } else { return i;
  • 3. } }; int index_buffer_by_array(const char buffer[], int index[][2], int w_counts[] ) { int word_cnt = 0; int len = strlen(buffer); int i = 0; int word_beg = 0; int word_end = 0; int is_word = 0; for (i = 0; i < len; i++) { if (!is_word && !isspace(buffer[i])) { word_beg = i; is_word = 1; } else if (is_word && isspace(buffer[i])) { word_end = i - 1; is_word = 0; int j; for (j = 0; j < word_cnt; j++) { if (word_end - word_beg != index[j][1] - index[j][0]) { continue; } int k; for (k = index[j][0]; k <= index[j][1]; k++) {
  • 4. if (buffer[word_beg + k - index[j][0]] != buffer[k]) { break; } } if (k == index[j][1] + 1) { w_counts[j]++; break; } } if (j == word_cnt) { index[j][0] = word_beg; index[j][1] = word_end; w_counts[word_cnt] = 1; word_cnt++; } } } return word_cnt; }; int find_word_by_array(int word_beg, const char buf[], int index[][2]) { for (int i = 0; index[i][WD_BGN] != -1; i++) { if (strncmp(&buf[word_beg], &buf[index[i][WD_BGN]], index[i][WD_END] - index[i][WD_BGN] + 1) == 0) { return i;
  • 5. } } return NOT_FOUND; }; void print_report_by_array(const char buf[], int index[][2], int counts[], int word_cnt) { for (int i = 0; i < word_cnt; i++) { printf("%d(%2d): %d %.*sn", i, index[i][WD_BGN], counts[i], index[i][WD_END] - index[i][WD_BGN] + 1, &buf[index[i][WD_BGN]]); } }; int index_buffer_by_ptr(const char * buf, const char * index[][2], int word_counts[] ) { int i = 0; int w_count = 0; while (*buf != '0') { if (!isspace(*buf)) { // Found a word const char *word_beg = buf; while (!isspace(*buf) && *buf != '0') { buf++; } const char *word_end = buf - 1; int index_found = find_word_by_ptr(word_beg, index); if (index_found == NOT_FOUND) { // New word found
  • 6. index[w_count][WD_BGN] = word_beg; index[w_count][WD_END] = word_end; word_counts[w_count] = 1; w_count++; } else { // Existing word found word_counts[index_found]++; } } else { buf++; } } return w_count; }; int find_word_by_ptr(const char *beg, const char *index[][2]) { int i, j; for (i = 0; index[i][WD_END] != NULL; i++) { if (index[i][WD_BGN] == NULL) { continue; } if (strlen(beg) != index[i][WD_END] - index[i][WD_BGN]) { continue; } for (j = 0; j < index[i][WD_END] - index[i][WD_BGN]; j++) { if (*(beg + j) != *(index[i][WD_BGN] + j)) { break; } } if (j == index[i][WD_END] - index[i][WD_BGN]) { return i; } }
  • 7. return NOT_FOUND; }; void print_report_by_ptr(const char *buf, const char *index[][2], int counts[], int word_cnt) { for (int i = 0; i < word_cnt; i++) { printf("%d(%2d): %d %.*sn", i, (int)(index[i][WD_BGN] - buf), counts[i], (int)(index[i][WD_END] - index[i][WD_BGN] + 1), index[i][WD_BGN]); } }; ......................................................................................................................... Ande here is the header file: .......................................................................................................................... #include <stdio.h> #include <ctype.h> #ifndef STRING_INDEX_HEADER #define STRING_INDEX_HEADER #define BUFFER_SIZE 2048 #define WORD_LIMIT 100 #define NOT_FOUND -1 #define WD_BGN 0 #define WD_END 1 // Prototypes /* initialize_buffer: Fills the given array with characters from stdin until either EOF or
  • 8. 2048 characters have been loaded. Returns the total number of characters loaded and -1 if no characters loaded. */ int initialize_buffer(char buffer[]); /******************************************************** Functions that work with arrays ******************************************************* */ /* index_buffer_by_array: Indexes the beginning and end of each unique word (string of non-whitespace characters). The index is a list of beginning and ending array indexes for each unique word found. Duplicate words are counted in w_counts array. Returns the total number of unique words found in the character buffer. */ int index_buffer_by_array(const char buffer[], int index[][2], int w_counts[] ); /* find_word_by_array: Determines if the word in buf[] beginning at index word_beg is already indexed. A word terminates with the first whitespace character. Returns the index number for the word if found, otherwise returns NOT_FOUND. */ int find_word_by_array(int word_beg, const char buf[], int index[][2]);
  • 9. /* print_report_by_array: Prints to stdout a report giving the word index number, offset of first letter from beginning of the buffer, the word count, and the word itself. This function expects index to be an array of array indexes for the beginning and end of each word. Example output for buffer containing the characters in quotes "all the all" 0( 0): 2 all 1( 4): 1 the */ void print_report_by_array(const char buf[], int index[][2], int counts[], int word_cnt); /******************************************************* Functions that work with pointers ********************************************************/ /* index_buffer_by_ptr: Indexes the beginning and end of each unique word (string of non-whitespace characters). The index is a list of beginning and ending char pointers for each unique word found. Duplicate words are counted in w_counts array. Returns the total number of unique words found in the character buffer. */ int index_buffer_by_ptr(const char * buf, const char * index[][2], int word_counts[] ); /* find_word_by_ptr:
  • 10. Determines if the word in beginning at the char * beg is already indexed. A word terminates with the first whitespace character. Returns the index number for the word if found, otherwise returns NOT_FOUND. */ int find_word_by_ptr(const char * beg, const char * index[][2]); /* print_report_by_ptr: Prints to stdout a report giving the word index number, offset of first letter from beginning of the buffer, the word count, and the word itself. This function expects index to be an array of char pointers for the beginning and end of each word. Example output for buffer containing the characters in quotes "all the all" 0( 0): 2 all 1( 4): 1 the */ void print_report_by_ptr(const char * buf, const char * index[][2], int counts[], int word_cnt); #endif ........................................................................................................................... Here is my current output (NOT WHAT I AM LOOKING FOR): 0( 0): 1 How 1( 4): 1 many 2( 9): 1 wood 3(14): 1 chucks 4(21): 1 would 5(27): 1 chuck 6(33): 1 wood 7(38): 1 if 8(41): 1 a 9(43): 1 wood
  • 11. 10(48): 1 chuck 11(54): 1 could 12(60): 1 chuck 13(66): 1 wood. 0( 0): 1 How 1( 4): 1 many 2( 9): 3 wood 3(14): 1 chucks 4(21): 1 would 5(27): 3 chuck 6(38): 1 if 7(41): 1 a 8(54): 1 could My array functions almost works, it is just cutting off the last word of the input and it counts words with an uppercase letter as different than a lowercase (e.g., 0(0) 1 All, 1(4) 1 all). Everything else about that is correct. My ptr functions are not working correctly. I am trying to show each unique word once with the count next to them. We are not using c string in this so don't use 'strcmp' please.