Thread: Need help.

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    21

    Need help.

    So this is what I need to do:
    https://i.imgur.com/aqw0tzg.png
    https://i.imgur.com/hok4ozL.png

    Im stuck at the return the avrage value of the substrings that is a match. Idk how to make an addition on these substrings that match.

    Here is what I made so far. Btw the int main here is not really important for what I´m asking.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    float my_compare(char str1[], char str2[], int indexStart, int indexEnd);
    
    int main() 
    {
        
        char str1[50];
        char str2[50];
        int a;
        int b;    
         
        
        printf("\nEnter the first string:");    
        fgets(str1,50,stdin);
    
        printf("\nEnter the second string:");
        fgets(str2,50,stdin);
    
        do{
    
            printf("\n(Start index can’t be bigger then the end index.)\nPlease enter the start index: ");
            scanf("%d",&a);
    
            printf("Please enter the end index: ");
            scanf("%d",&b);
            
    }   while (b<a);
        
         int c = my_compare(str1, str2, a, b);
    }
    
    float my_compare(char str1[], char str2[], int indexStart, int indexEnd)
    { 
        for(int i=indexStart; i<indexEnd; ++i) //this is the substring for both str1 and str2
        {
            if(str1[i]==str2[i]) //only the same characters
            {    
            printf("\n %4c --> %4d\n ", str1[i], str1[i]);
            }
            else {
            }
        }
    
    }
    Last edited by JohnnyC; 10-30-2018 at 06:35 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you know how to do an average in general? As your printf is showing you, you can treat str1[i] as a numerical value, so you don't have to do any conversions to get the ASCII values.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    You mean generally in math? then yes. Sum divided by number of elements.
    But idk how to make a sum of the characters that are str1[i]==str2[i].
    Thanks for the reply

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're already doing the check whether str[1]==str[2]; inside that if statement you can add to a running total, and also add 1 to the count of matched characters. (You'll have to initialize total and count to zero before you start, of course.)

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    Shortly after you told me that I managed to do it, I looked online how someone else did the average and saw he did something similar to what I tried but the only thing was I wrote float where I shouldn´t so it didn´t work.
    But anyways here it is

    Code:
    #include <stdio.h>
    #include <string.h>
    
    float my_compare(char str1[], char str2[], int indexStart, int indexEnd);
    
    int main() 
    {
        
        char str1[50];
        char str2[50];
        int a;
        int b;    
         
        
        printf("\nEnter the first string:");    
        fgets(str1,50,stdin);
    
        printf("\nEnter the second string:");
        fgets(str2,50,stdin);
    do{
        printf("\n(Start index can’t be bigger then the end index.)\nPlease enter the start index: ");
        scanf("%d",&a);
    
        printf("Please enter the end index: ");
        scanf("%d",&b);
            
    }    while (b<a);
        
         int c = my_compare(str1, str2, a, b);
    }
    
    float my_compare(char str1[], char str2[], int indexStart, int indexEnd)
    { 
    
        float sum = 0;
        float e = 0;
        float avrage = 0;
    
        for(int i=indexStart; i<indexEnd; ++i) //this is the substring for both str1 and str2
        {
            if(str1[i]==str2[i]) //only the same characters
            {    
            printf("\n %4c --> %4d\n ", str1[i], str1[i]);
            sum += str1[i];
            ++e; 
            }
        
        }
        avrage = sum/e;
        printf("The average ASCII sum of the compared strings is: %f\n",avrage);
    }
    Thank you for helping me

Popular pages Recent additions subscribe to a feed

Tags for this Thread