SlideShare a Scribd company logo
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
 
Ponters
PontersPonters
Ponters
Anil Dutt
 
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
Puskar Bhandari
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
C programming
C programmingC programming
C programming
Karthikeyan A K
 
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
eyewaregallery
 
See through C
See through CSee through C
See through C
Tushar B Kute
 
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
Ramakrishna 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.docx
DIPESH30
 
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
rahulfancycorner21
 
Types of pointer
Types of pointerTypes of pointer
Types of pointer
Ravindra Nikate
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
rgnikate
 
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
sukhvir71
 
Quiz 9
Quiz 9Quiz 9
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
NewsMogul
 
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
arenamobiles123
 
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
namarta88
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Khan
 
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
Kevlin 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.pdf
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 
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
sonunotwani
 

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

Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 

Recently uploaded (20)

Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 

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.