Question

Answer all the following questions for code in C for thumbs up! Do not just post...

Answer all the following questions for code in C for thumbs up!

Do not just post solution/code without explanation that'll waste everyones time, and my hard earned money!

1. What does the function calculate_modified do?
2. What problems or issues, if any, do you see with it?
3. How could you improve the function, and what benefits would your improvements make?
4. What other additional improvements could you make to the rest of the code?

This code is intentionally flawed in response improving the function to be more efficient while ensuring the input parameters are the same, and output matches what it would have been before the changes were made. Only looking for efficiency improvements.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>

typedef int int32;
typedef char int8;

static void unite_arrays(int8 *output, int8 *input_1, const int8 *input_2, int32 length)
{
int32 i = 0;

for(i=0; i<length; i++)
{
output[i] = (int8)(input_1[i] ^ input_2[i]);
}

return;
}

static void itoa( int32 num, int8 *alpha, int32 radix )
{
if( radix == 10 )
{
sprintf(alpha, "%i", num);
}
else if( radix == 16 )
{
sprintf(alpha, "%X", num);
}
}

int8 *calculate_modified(int8 modifier, const int8 *input_1, int32 length)
{
int8 leading[3];
int32 i_leading;
int8 * temp_string = NULL;
int8 * ret;
int32 i = 0;

itoa(modifier/2, leading, 10);
i_leading = atoi(leading);
temp_string = (int8 *) malloc(8);
ret = (int8 *) malloc(length);
memset(temp_string, 0, 8);
temp_string[0] = 0;

if( (modifier+1)%2 == 0 ) {
temp_string[0] = (int8)((i_leading<<4) + 8);
}   
else {
temp_string[0] = (int8)(i_leading<<4);
}   

for(i=0; i<(length>>3); i++)
{
unite_arrays(ret+i*8, temp_string, input_1+i*8, 8);
}   
free(temp_string);
return ret;
}   

// Do not change anything in main
// main only contains example input
// Change anything else
int main(int argc, char **argv)
{
int8 data[] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57};

int8 *resp = calculate_modified(0x10, data, sizeof(data));
free(resp);

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please comment below, If you find any difficulty in understanding

Please Upvote the answer, If you are impressed

Answer :

#include <iostream>
#include <vector>

using namespace std;

int main()
{
   //n--> number of strings
   int n;
   cin >> n;
   //words--> taking input strings
   vector<string> words(n);
   //frequency--> taking frequency of words
   vector<int> frequency(n);
   for (int i = 0; i < n; ++i)
   {
       cin >> words[i];
       frequency[i] = 0;
   }
   //now logic
   for (int i = 0; i < n; ++i)
   {
       int cnt = 1;
       int flag = 0;
       for (int j = 0; j < n; ++j)
       {
           //if not in same position then only we have to increment
           if(i!=j)
           {
               // if we calculate it value already then no need of calculate it value again
               if((words[i]==words[j]) && frequency[j]!=0)
               {
                   frequency[i] = frequency[j];
                   flag = 1;
                   break;
               }
               //if we didn't calculate it value we have to calculate
               else if((words[i]==words[j]) && frequency[j]==0)
               {
                   ++cnt;
               }
           }
       }
       //if we didn't calcaulte it frequency value already then only we have to assign frequency value
       if(flag==0)
       {
           frequency[i] = cnt;
       }
       //frequency[i] = cnt;
   }
   for (int i = 0; i < n; ++i)
   {
       cout << words[i] << " " << frequency[i] << "\n";
   }
   return 0;  
}

Add a comment
Know the answer?
Add Answer to:
Answer all the following questions for code in C for thumbs up! Do not just post...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • C Question: I apologize for this long size post, but I wanted to explain the way...

    C Question: I apologize for this long size post, but I wanted to explain the way of doing it. Kindly HELP me . Question is: implement char *mystrcat(char *dest, const char*src)   adds a copy of the string src onto the end of the string starting at dest. The first char is src is put at the location of the 0 at the end of string dest. Returns dest Could you please implement / write the code in the same way...

  • this is c code. please answer all questions on a piece of paper and show work....

    this is c code. please answer all questions on a piece of paper and show work. i need to prepare as i have a midterm i will have to be completing on paper 1) Bit Operators: This C program compiles and runs. What is its output? 1) #include <stdio.h> 2) void main (void) 3) unsigned char x =60; 4) 5) 6) 7) 8 ) 9) 10) 11) 12) 13) unsigned char a = x < 1; unsigned char b unsigned...

  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

  • Fill in the missing C code according to the comments. #include <stdio.h> typedef struct {   ...

    Fill in the missing C code according to the comments. #include <stdio.h> typedef struct {    int num;    char name[25]; } student; int main(){    student s1;           //assign the value of 1234 to num in the s1 structure    //assign the value of Bob the Builder to name in s1 structure       //display the value of num in the s1 structure    //display the value of name is the s1 structure student *s2;       ...

  • T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer...

    T/F C Language Questions. Answer the following true/false questions. You must correctly state WHY your answer is true or false in order to receive credit. #include <stdio.h> #include <string.h> int run_through(int num, char **a) { int i; int check=0; for(i=0;i<num;i++) { printf("%s\n", *(a+i)); if(strcmp(*(a+i), "filename")==0) { check=1; } } return check; } char** find_filename(int n, char **b) { int i; int check=0; for(i=0;i<n;i++) { if(strcmp(*b, "filename")==0) { b++; break; } b++; } return b; } int main(int argc, char **argv)...

  • Help with C++ reverse program with leading zeros. I need to put the line from the...

    Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code. #include <iostream> #include <cstdlib> #include <iostream> using std::cout; using std::cin; using std::endl; //function templates int reverseNum(int); int main() { //variables char buf[100]; int num; while (true) { //prompt user for input cout << "Enter the number...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT