170k views
1 vote
The getLikes() function will, given a list of users, return the number of times a given user has liked a post by a given author. This function should find the firs post in the posts array by the author. Note this is a standalone function rather than a class member function.

This function should:
• Have four parameters in this order:
•string post_author: author of post in question
• Post posts[]: array of Post objects
oint num_posts_stored: the number of posts currently stored in the array
• string username: user who liked the post in question
• User users[]: array of User objects
oint num_users_stored: the number of users currently stored in the array
• The username search should be case sensitive. For example, "Ben", "ben" and "BEN" all refer to the different user.
• The function behavior should match the following scenarios:
• Return the value of likes from the user for the post posted by post_author
• Return-3 if the user or the post author are not found
• Return-2 if the number of users or number of posts stored are 0 (or less than 0)

User Liangzan
by
7.9k points

1 Answer

3 votes

Final answer:

The getLikes() function returns the number of likes from a user for a post by a given author, validates the existence of users and posts, and is case-sensitive for usernames.

Step-by-step explanation:

The getLikes() function you mentioned would be used to track user interactions in a social media context by returning the number of likes a specific user has given to a post from a particular author. The function should consider several cases. If the user or the post author doesn't exist in the dataset, the function should return -3. If there are no users or posts, indicated by having zero or a negative number of users or posts stored, the function should return -2. Otherwise, the function should search through the array of Post objects to find the first post by the specified author and then return the number of likes that specific user has given to that post.

Here is how the logic should be implemented:

  • Verify that the number of posts and users stored are greater than 0.
  • Search for a post by the given author.
  • If the post is found, search for the user in the array of User objects.
  • If both the post and user are found, return the number of likes the user has given to the post.
  • Otherwise, return the appropriate error code.

The function is designed to be case-sensitive, thus different capitalizations would indicate different users.

User Selrac
by
7.9k points