Question

In: Computer Science

This is C++ programming Given an int variable k, an int array incompletes that has been...

This is C++ programming

Given

  • an int variable k,
  • an int array incompletes that has been declared and initialized,
  • an int variable nIncompletes that contains the number of elements in the array,
  • an int variable studentID that has been initialized, and
  • an int variable numberOfIncompletes,

Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes.

You may use only k, incompletes, nIncompletes, studentID, and numberOfIncompletes.

I tried this code down below but it's giving me a logical error:

numberOfIncompletes=0;
for (k = 0; k < nIncompletes; k++)

numberOfIncompletes=0;
for (k = 0; k < nIncompletes; k++)
{

if (incompletes[k] == studentID)
numberOfIncompletes ++;
numberOfIncompletes=studentID;
}

Solutions

Expert Solution

In above code, your mistake is you are incrementing numberOfIncompletes while found studentID in incompletes and you are also assigning studentID to numberOfIncompletes. That's wrong. You only need to increment count.

when you are performing numberOfIncompletes++, it will perform numberOfIncompletes = numberOfIncompletes + 1. Means it directly assigns to numberOfIncompletes. So here your problem is solved.

When you are writing numberOfIncompletes=studentID means every time it will give you studentID in numberOfIncompletes. That is wrong.

You can refer below code for your reference,

Here student ID is 2 and in incompletes it repeats 3 times. Thus, count of numberOfIncompletes becomes 3.

#include <iostream>

using namespace std;

int main()
{
    int k;
    int incompletes[10] = {1, 2, 4, 2, 3, 15, 8, 7, 1, 2};
    int nIncompletes = 10;
    int studentID = 2;
    int numberOfIncompletes = 0;
    
    for (k = 0; k < nIncompletes; k++)
    {
        //when studentID matches,increment numberOfIncompletes by 1 
        if (incompletes[k] == studentID)
        numberOfIncompletes++;
        
    }
    cout<<numberOfIncompletes;

    return 0;
}

Please refer below screenshot of code and output.

In your problem statement it is said that Write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes. Here assigns the value means whatever count we are getting that assign to numberOfIncompletes. So, that's your mistake.


Related Solutions

Use C Programming - Given an array of integers and a number K, find the smallest...
Use C Programming - Given an array of integers and a number K, find the smallest element in array greater than or equal to K. If such element exists in the array, display it otherwise display "-1". Example: Input:     8     1 3 4 7 8 9 9 10     5     where: First line represents the number of elements in the array. Second line represents the elements in the array. Third line represents the value of K. Output:     7 Explanation:...
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
// Given an int array of size elements, determine if there are k elements that add...
// Given an int array of size elements, determine if there are k elements that add up to sum. // The array holds integers, both positive and negative and zero. // It is not possible to add zero elements (that's when k==0) to any sum, not even zero. // It is not possible to add any elements from an empty array. // Must be recursive and not iterative //bool K_element_sum(size_t k, int sum, int arr[], size_t size){}
A.) myNums is an array of 50 elements of type int and k is an int...
A.) myNums is an array of 50 elements of type int and k is an int variable. For which one we get the index of out of bounds? a. for (k = 0; k <= 49; k++)     cout << myNums[k] << " "; b. for (k = 1; k < 50; k++)     cout << myNums[k] << " "; c. for (k = 0; k <= 50; k++)     cout << myNums[k] << " "; d. for (k =...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a...
Programming in C:- 1. Suppose that a variable 's' is an array of strings (i.e. a pointer array where each element points to a string). Write an expression that obtains the length of the third string of 's'. You may assume that string.h has been included. 2. Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3: #include <string.h> #include <stdlib.h> char**...
Given an int variable k, write an expression whose value is the k-th character in the String variable word.
1. Given an int variable k, write an expression whose value is the k-th character in the String variable word. Assume thatword has been initialized and that the value ofk is non-negative and less than the length ofword.Example 1: if k's value is 3 andword is "spring", the value of the expression would be 'i'.Example 2: if k's value is 0 andword is "fall", the value of the expression would be 'f'.2. Given an int variable k, write an expression whose...
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
Write a program on C defining an array. Find the element at given index k. Replace the element at given index k.
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT