Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


NUCLEAR RABBIT

Member Since 12 Apr 2006
Offline Last Active Jan 28 2015 11:07 PM

Topics I've Started

Polymorphism and Interfaces in Java

07 October 2014 - 02:23 PM

Hello,

 

So I recently learned about Interfaces and thought I had a good grasp, but I'm not sure why this example does what it does. It creates an interface object and assign it to an ArrayQueue<T>() object. Why not just write it as an ArrayQueue object to begin with, is there an advantage? I think my question relates more to polymorphism maybe, but I also thought that interfaces aren't objects that are initialized, just implemented from in another class.

 

Can anyone help clear the air? I would really appreciate any help! huh.png


Writing/Reading from files help needed! (C Programming)

21 May 2014 - 05:13 PM

hello, I am trying to write a program that makes 10 empty entries in my file, updating the first 3, and then asking the user which one to erase back to a blank entry, and then reprints the file contents again with the entry deleted. It seems to work okay ONLY if you delete the first entry, if you try to delete the 2nd or 3rd, the output goes all crazy! Any help would be greatly appreciated! :(

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

typedef struct person {
    char first_name[15];
    char last_name[15];
    char age[4];
    int accNum;
} Person;

int main(int argc, char * argv[])
{
    FILE * file = fopen("/Users/MYNAME/Desktop/person.txt", "r+");
    
    Person blankEntry = {"No First Name", "No Last Name", "0", 0};
    Person person1 = blankEntry;
    
    printf("Records:\n");
    for(int i = 1; i <= 10; i++) {
        fwrite(blankEntry.first_name, 1, sizeof(blankEntry.first_name), file);
        fwrite(blankEntry.last_name, 1, sizeof(blankEntry.last_name), file);
        fwrite(blankEntry.age, 1, sizeof(blankEntry.age), file);
        fprintf(file, "%d", i);
        
        printf("%d. %s\t\t\t%10s%10s\n", i, blankEntry.first_name, blankEntry.last_name, blankEntry.age);
    }
    
    printf("\n\nUpdating Record:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 3; i++) {
        printf("Enter in first name, last name, and age: ");
        scanf("%s %s %s", person1.first_name, person1.last_name, person1.age);
        
        fwrite(person1.first_name, 1, sizeof(person1.first_name), file);
        fwrite(person1.last_name, 1, sizeof(person1.last_name), file);
        fwrite(person1.age, 1, sizeof(person1.age), file);
        fprintf(file, "%d", i);
    }
    
    printf("\n\nRecords:\n");
    printf("First Name:\t\tLast Name:\t\tAge:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 10; i++) {
        fread(person1.first_name, 1, sizeof(person1.first_name), file);
        fread(person1.last_name, 1, sizeof(person1.last_name), file);
        fread(person1.age, 1, sizeof(person1.age), file);
        fscanf(file, "%d", &person1.accNum);
        
        printf("%i. %s\t\t%10s%10s\n", person1.accNum, person1.first_name, person1.last_name, person1.age);
    }
    
    int deleteKey;
    printf("\n\nEnter in account number to delete: ");
    scanf("%i", &deleteKey);
    
    fseek(file, (deleteKey - 1) * sizeof(struct person), SEEK_SET);
    fwrite(person1.first_name, 1, sizeof(person1.first_name), file);
    fwrite(person1.last_name, 1, sizeof(person1.last_name), file);
    fwrite(person1.age, 1, sizeof(person1.age), file);
    //fwrite(&blankEntry, sizeof(struct person), 1, file);
    
    printf("\n\nRecords:\n");
    printf("First Name:\t\tLast Name:\t\tAge:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 10; i++) {
        fread(person1.first_name, 1, sizeof(person1.first_name), file);
        fread(person1.last_name, 1, sizeof(person1.last_name), file);
        fread(person1.age, 1, sizeof(person1.age), file);
        fscanf(file, "%d", &person1.accNum);
        
        printf("%i. %s\t\t%10s%10s\n", person1.accNum, person1.first_name, person1.last_name, person1.age);
    }
    
    fclose(file);
    
    return 0;
}

strchr question (C programming)

27 April 2014 - 02:51 PM

Hello, so I was writing a program to print only words that end with "ed" and I thought that the last element in a char array was '\0' so I thought adding \0 to the end of the searchKey would help detect the end of the string but I guess I went about doing this wrong. What would you suggest I look into to accomplish what I'm trying to do?

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(int argc, const char * argv[])
{
    char * lib[] = {"apple", "bat", "create", "destroy", "build"};
    int lib_size = sizeof(lib)/sizeof(lib[0]);
    char * searchKey = "b";
    char * wordPtr[lib_size];
    
    for(int i = 0; i < lib_size; i++)
    {
        wordPtr[i] = lib[i];
    }
    
    size_t searchSize = 1;
    
    printf("Word Bank:\n+ ");
    
    for(int i = 0; i < lib_size; i++)
    {
        printf("%s ", lib[i]);
    }
    
    printf("\n\nWords beginning with 'b':\n+ ");
    
    for(int i = 0; i < lib_size; i++)
    {
        if(strncmp(wordPtr[i], searchKey, searchSize) == 0)
        {
            printf("%s ", wordPtr[i]);
        }
    }
    
    //----------------------------------------------------------------
    
    printf("\n\nNew Word Bank:\n+ ");
    
    char * ed_lib[] = {"walke", "ededededs", "paniced", "play", "music", "education"};
    int ed_size = sizeof(ed_lib)/sizeof(ed_lib[0]);
    char * ed_searchKey = "ed\0";
    
    for(int i = 0; i < ed_size; i++)
    {
        printf("%s ", ed_lib[i]);
    }
    
    printf("\n\nWords ending in 'ed'\n+ ");
    
    char * edPtr = ed_lib;
    
    for(int j = 0; j < ed_size; j++)
    {
        edPtr = ed_lib[j];
        
        if(strstr(&edPtr[j], ed_searchKey) != NULL)
        {
            printf("%s ", ed_lib[j]);
        }
    }
    
    puts("");
    
    return 0;
}

Array Location? (C Programming)

23 April 2014 - 01:22 PM

Hello, I am writing a program that takes a sentence and tranforms that sentence into the morse code equivalent. Problem is, when I print the text after the convertion, all the letters after the 1st letter are the same morse code symbol, instead of the correct letter equivalent :/ I been looking over my code trying to see why this is happening, but I cannot see what the problem can be! Can anyone please help me see what I am doing wrong? I'd surely appreciate any help! ohmy.png

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LETTERS 26
#define NUMBERS 10

void convertToLower(char * sPtr);

int main(int argc, const char * argv[])
{
    // Morse Code 0-9
    //char * num_codes[] = {"-----", "----.", "---..", "--...", "-....", ".....", "....-", "...--", "..---", ".----"};
    // Morse Code A-Z
    char * alp_codes[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    // A-Z
    const char searchKey[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    
    // string variables & attributes
    char random_text[] = "abcd";
    char * txtPtr = random_text;
    int text_size = sizeof(random_text)/sizeof(random_text[0]);
    char * morse_code[text_size];
    int alp_index;
    
    convertToLower(random_text);
    
    // seaches for the searchKey in the txt and
    // converts that character of random_txt to the alp_code
    for(int h = 0; h < text_size; h++)
    {
        for(int i = 0; i < LETTERS; i++)
        {
            txtPtr = strchr(txtPtr, searchKey[i]);
            alp_index = (int)(txtPtr - random_text);
            
            if(txtPtr != NULL)
            {
                txtPtr++;
                morse_code[h] = alp_codes[alp_index];
                break;
            }
        
            txtPtr = random_text;
        }
    }
    
    // prints morse code
    for(int i = 0; i < text_size-1; i++)
    {
        printf("%s | ", morse_code[i]);
    }
    
    return 0;
}

//----------------------------------------------------------------------------------------
//  FUNCTION DEFINITIONS

void convertToLower(char *sPtr)
{
    while(*sPtr != '\0')
    {
        *sPtr = tolower(*sPtr);
        *sPtr++;
    }
}

String help in C

16 April 2014 - 09:25 PM

Hello, I am trying to write a program that counts how many times a letter occurs in a string. I know there's easy ways to do it, but I want to use the strchr function to have a search key. I wrote this and for some reason, nothing even happens when I run it! No errors or nothing so I'm not sure what the problem could be! Any help is appreciated sad.png

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{
    const char * random_text = "Once there was this really old man who tranformed into dust. The end!";
    
    int counter = 0;
    int searchKey = 'd';
    char * strPtr = strchr(random_text, searchKey);
    
    do {
        if(strPtr != NULL)
           counter++;
        
        strPtr = strchr(random_text+1, searchKey);
    } while (strPtr != NULL);
    
    printf("%s\n%s\n\n%s%c%s%d%s", "The text:", random_text, "The occurence of ",
           searchKey, " happened ", counter, " times.\n");
    
    return 0;
}

PARTNERS