New to Programming ;D

Hey Guys!

I'm kind of new to programming and my professor gave me an assignment I don't know how to complete, is there any one of you that could help me out?
I'd really appreciate it :D


Here's the main.cpp

The //* comments is the stuff we have to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 #include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>


#include "arrayUtils.h"

using namespace std;

// Strips all punctuation (except hyphens) from a word and
// changes any upper-case characters to lower-case. Note that
// if a word has nothing except punctuation, this may reduce
// it to the empty string.
string reduceWords (string word)
{
  string result;
  for (int i = 0; i < word.size(); ++i)
    {
      char c = word[i];
      if (c == '-')
	result += c;
      else if (c >= 'a' && c <= 'z')
	result += c;
      else if (c >= 'A' && c <= 'Z')
	result += (c - 'A' + 'a'); // converts to lower-case
    }
  return result;
}

// Read words from the provided input stream, reducing each word and counting
// how many times each word appears. Write the resulting words and counts (in
// alphabetic order by word) in CSV format to the output stream.
// - Assume that the input contains a maximum of MaxWords distinct words
//   (after reduction). If more distinct words than this are actually
//   encountered, write nothing to the output stream but print an error
//   message on the standard error stream.
void histogram(const int MaxWords, istream& input, ostream& output)
{


  //* Declare an array of strings to hold all words
  //*   and an array of int to hold the number of times each word
  //*   encountered.‡‡‡‡‡
std ::string allwords[MaxWords];              // This is the part I was working 
int wordcount [MaxWords];                     // on I think I got it right
int count = 0;

  // Read the words from the input and count them
  string word;
  while (input >> word)
    {                               // This is the part I don't know how to do

      //* Reduce the word and, if any characters are left
      //* check to see if the word in already in our array
        
      //* If so, add one to that word's counter
      //* If not, is there room to add it to the array?

      //**  If so, add the word and a counter to the arrays, setting the
      //**     counter to 1.
      //**  If not, print the error message and abort the program [exit(1);]

    }

  //* Print all the words found, with their counts, in .csv format.

  //* Clean up the arrays we created
}



int main (int argc, char** argv)
{
  if (argc != 2 && argc != 4)
    {
      cerr << "Usage: " << argv[0] << " MaxWords [inFileName outFileName]" << endl;
      return -1;
    }

  int MaxWords = atoi(argv[1]);

  if (argc == 2)
    {
      // No file names in command line - use standard in and standard out
      histogram (MaxWords, cin, cout);
    }
  else
    {
      // Take input and output file names from the command line
      ifstream in (argv[2]);
      ofstream out (argv[3]);
      histogram (MaxWords, in, out);
    }

  return 0;
} 


He also gave us a header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef ARRAYUTILS_H
#define ARRAYUTILS_H



//  Add to the end
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger
//      than the current value of that counter
template <typename T>
void addToEnd (T* array, int& size, T value)
{
   array[size] = value;
   ++size;
}



// Add value into array[index], shifting all elements already in positions
//    index..size-1 up one, to make room.
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger
//      than the current value of that counter

template <typename T>
void addElement (T* array, int& size, int index, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= index) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[index] = value;
  ++size;
}


// Assume the elements of the array are already in order
// Find the position where value could be added to keep
//    everything in order, and insert it there.
// Return the position where it was inserted
//  - Assumes that we have a separate integer (size) indicating how
//     many elements are in the array
//  - and that the "true" size of the array is at least one larger
//      than the current value of that counter

template <typename T>
int addInOrder (T* array, int& size, T value)
{
  // Make room for the insertion
  int toBeMoved = size - 1;
  while (toBeMoved >= 0 && value < array[toBeMoved]) {
    array[toBeMoved+1] = array[toBeMoved];
    --toBeMoved;
  }
  // Insert the new value
  array[toBeMoved+1] = value;
  ++size;
  return toBeMoved+1;
}


// Search an array for a given value, returning the index where
//    found or -1 if not found.
template <typename T>
int seqSearch(const T list[], int listLength, T searchItem)
{
    int loc;

    for (loc = 0; loc < listLength; loc++)
        if (list[loc] == searchItem)
            return loc;

    return -1;
}


// Search an ordered array for a given value, returning the index where
//    found or -1 if not found.
template <typename T>
int seqOrderedSearch(const T list[], int listLength, T searchItem)
{
    int loc = 0;

    while (loc < listLength && list[loc] < searchItem)
      {
       ++loc;
      }
    if (loc < listLength && list[loc] == searchItem)
       return loc;
    else
       return -1;
}


// Removes an element from the indicated position in the array, moving
// all elements in higher positions down one to fill in the gap.
template <typename T>
void removeElement (T* array, int& size, int index)
{
  int toBeMoved = index + 1;
  while (toBeMoved < size) {
    array[toBeMoved] = array[toBeMoved+1];
    ++toBeMoved;
  }
  --size;
}



// Search an ordered array for a given value, returning the index where
//    found or -1 if not found.
template <typename T>
int binarySearch(const T list[], int listLength, T searchItem)
{
    int first = 0;
    int last = listLength - 1;
    int mid;

    bool found = false;

    while (first <= last && !found)
    {
        mid = (first + last) / 2;

        if (list[mid] == searchItem)
            found = true;
        else
            if (searchItem < list[mid])
                last = mid - 1;
            else
                first = mid + 1;
    }

    if (found)
        return mid;
    else
        return -1;
}





#endif 



Thanks Guys!
Candice xoxoxoxo
Help would be really awesome I'm still stuck :(
closed account (zb0S216C)
Will it blend? Seriously, what are you having problems with? Welcome to the forum, by the way :)P

Wazzak
I'm having trouble writing the code for the instructions my teacher gave me :(
The instructions are in the //* comments I don't know how to begin what he told us to do, I guess he wants us to sort the array according to the comments but I don't know how. He also wants us to put it into the .csv output. I'm just really confused since I'm new to programming. Is there anyway you can help me ?
So do you think you could help me Framework?
1
2
3
4
5
  //* Declare an array of strings to hold all words
  //*   and an array of int to hold the number of times each word
  //*   encountered.‡‡‡‡‡
std ::string allwords[MaxWords];              // This is the part I was working 
int wordcount [MaxWords];                     // on I think I got it right 

Technically, that is not standard C++. You cannot create an array with a length not known at compile time. If you're not careful with settings, some compilers (i.e. GCC) will let it slide. Variable length arrays are allowed in the C99 standard but not in C++.

Here, you will need to use dynamic memory allocation. For instance, the following creates an array of ints dynamically and destroys it once it is no longer used.

1
2
3
4
5
6
7
8
9
10
11
int size_of_array = some_number(); //some number not known at compile time

int* arrayOfInts = new int[size_of_array]; //create an array of ints with size size_of_array

//assigning to the array
arrayOfInts[0] = 1;
arrayOfInts[1] = 2;
//...

//we're done using it, so destroy the array
delete [] arrayOfInts;


//* Reduce the word and, if any characters are left

Here, the function reduceWords can help you. You can use the std::string.size() function to query how many characters are left in the std::string.

//* check to see if the word in already in our array

Here, you can use the seqSearch function.

//** If so, add the word and a counter to the arrays

Here, you can use the addToEnd function. But I guess he wants you to keep them sorted so maybe you'll have to use the addInOrder function (you will have to shift your word counts to keep them correct).

//* Print all the words found, with their counts, in .csv format.

So, your file should look like this:

1
2
3
4
word1, count1
word2, count2
word2, count3
...


For file I/O, check this out: http://www.cplusplus.com/doc/tutorial/files/
Last edited on
shacktar everything you said made sense but how using dynamic arrays with ints going to help with strings? Is it possible to do the same thing with strings? Also all your code below the dynamic allocation assumes that everything is already in the array so wouldn't she need to figure how to store everything in the array? it would be greatly appreciated if you could help me understand here.
Is it possible to do the same thing with strings?

Yes.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>

int size_of_array = some_number(); //some number not known at compile time

std::string* arrayOfStrings = new std::string[size_of_array]; //create an array of strings with size size_of_array

//assigning to the array
arrayOfStrings[0] = "The first string";
arrayOfStrings[1] = "The second string";
//...

//we're done using it, so destroy the array
delete [] arrayOfStrings;


Also all your code below the dynamic allocation assumes that everything is already in the array so wouldn't she need to figure how to store everything in the array?

The dynamic allocation only puts aside the memory. At first in the histogram function, right after the allocation there will be space for MaxWords strings, however it will all be uninitialized. Notice how there's a count variable (int count = 0;) in the histogram function? This will be used to store the current size of the array. As words get added, this variable gets incremented, and it is also the size that will be passed into the helper functions such as seqSearch and addInOrder.
thanks i tried that and added some other things of my own and the code worked pretty well still trying to work on the word count but thanks for the help.
Topic archived. No new replies allowed.