Question

C Programming Question

Hi, I have the following code and in my submission I'm supposed to only include function definitions (i.e. implementations) in the file. However, I'm not NOT required to write the main, struct definitions and function prototypes.

Could someone help me fix this code?

#include<stdio.h> #1 nclude<stdlib.h> #include <string.h> struct ip address [ int octet_1; int octet_2; int octet_3; int octe

int is.validCip if(ip1.octet 10 && ip1.octet_1>255)1 return 0; address t ip1)1 if(ipi.octet.2 < 0 && ip1.octet.2 > 255){ retu

return 1; int isequalCip.adress.t ipl, ip.adress.t ip2)0 ifCip1.octet_1- ip2.octet_1)1 return 0; ifCip1.octet_2!- ip2.octet_2

return 0; return 1; int issame_subnet(ip_address_t ip1, ip_address t ip2)1 ifCip1.octet_1- ip2.octet_1)1 return 0; ifCip1.oct

return 1; int get_valid_ip addresss(ip_address.t addr_arry, int addr_arry_len)t int cnt = 0; for(int i-0; i<addr_arry_len; i+

int a[4]; while(splitvalue != NULL)f a[size] - atoi (splitvalue; splitvalue strtokCNULL,.); size++ if(size == 4){ ip addres

addr_arry[cnt] ip; cnt++i else printfCInvalid Input \n); elset printfCInvalid Input \n); return cnt; void print_same_subn

for(int i-0; i <addr_arry_len; i++)I arr1 i] - 1; int j = 0; for(int 1-0; ip_address_t i<addr-arry-len && ナaddr-arry-len; i++

ifCissame_subnet(ip1,ip2))[ printf , print.ip-addressCip2); arr1[j] -0; printfCn) int mainO ip_address t addr_arry[10]; i

printf(please enter How many Ip Address \n); scanf(%d, &lenvalue); int ent -get-valtd.ip.odresss(oddr-arry, lenvalue): pr

Text Version:

#include<stdio.h>

#include<stdlib.h>

#include <string.h>

struct ip_address {


int octet_1;

int octet_2;

int octet_3;

int octet_4;



};


    typedef struct ip_address ip_address_t;
    void print_ip_address(ip_address_t ip1){

    printf("%d.%d.%d.%d", 
    ip1.octet_1,ip1.octet_2,ip1.octet_3,ip1.octet_4);
    }

int is_valid(ip_address_t ip1){

    if(ip1.octet_1 < 0 && ip1.octet_1 > 255){
    return 0;
    }

    if(ip1.octet_2 < 0 && ip1.octet_2 > 255){
    return 0;
    }

    if(ip1.octet_3 < 0 && ip1.octet_3 > 255){
    return 0;
    }

    if(ip1.octet_4 < 0 && ip1.octet_4 > 255){
    return 0;
    }

return 1;
    }

int isequal(ip_address_t ip1, ip_address_t ip2){

    if(ip1.octet_1 != ip2.octet_1){
    return 0;
    }

    if(ip1.octet_2 != ip2.octet_2){
    return 0;
    }

    if(ip1.octet_3 != ip2.octet_3){
    return 0;
    }

    if(ip1.octet_4 != ip2.octet_4){
    return 0;
    }

return 1;

}

int issame_subnet(ip_address_t ip1, ip_address_t ip2){

    if(ip1.octet_1 != ip2.octet_1){
    return 0;
    }

    if(ip1.octet_2 != ip2.octet_2){
    return 0;
    }

    if(ip1.octet_3 != ip2.octet_3){
    return 0;
    }

return 1;

}

int get_valid_ip_addresss(ip_address_t addr_arry[], int addr_arry_len){

int cnt = 0;
int i;
    for(i=0; i<addr_arry_len; i++){
    char input[50];
    scanf("%s", input);

    if(!strcmp(input, "-1.-1.-1.-1")){

    return cnt;
    }

char *splitvalue = strtok(input, ".");

int size = 0;

int a[4];

    while(splitvalue != NULL){
    a[size] = atoi(splitvalue);
    splitvalue = strtok(NULL,".");
    size++;

}

if(size == 4){

    ip_address_t ip;
    ip.octet_1 = a[0];
    ip.octet_2 = a[1];
    ip.octet_3 = a[2];
    ip.octet_4 = a[3];

if(is_valid(ip)){

    addr_arry[cnt] = ip;

    cnt++;

}

    else
    printf("Invalid Input \n");

}

else{
printf("Invalid Input \n");

}

}

return cnt;

}

void print_same_subnet(const ip_address_t addr_arry[], int addr_arry_len){

int arr1[addr_arry_len];
int i;
    for(i=0; i<addr_arry_len; i++){

    arr1[i] = 1;

    }

int j = 0;

    for(i=0; i<addr_arry_len && j<addr_arry_len; i++){
    ip_address_t ip1 = addr_arry[i];

    if(arr1[i] == 1){
    print_ip_address(ip1);

    arr1[i] = 0;

}

    for(j = i+1; j <addr_arry_len; j++ ){
    if(arr1[j] == 1){

    ip_address_t ip2 = addr_arry[j];
    if(issame_subnet(ip1,ip2)){

    printf(",");

    print_ip_address(ip2);

    arr1[j] = 0;

}

}

}

printf("\n");

}

}

int main(){

ip_address_t addr_arry[10];

int lenvalue = 5;

    printf("please enter How many Ip Address \n");
    scanf("%d", &lenvalue);

int cnt = get_valid_ip_addresss(addr_arry, lenvalue);

    printf("Valid Ip Address count is: %d \n", cnt);

    printf("Same Subnet ip_addressess Are \n");

    print_same_subnet(addr_arry, cnt);

return 0;

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

//C code doesn't work without main, but since you have requested for only the function definitions, writing them below::

//1. Function to check if the IP address is valid

int is_valid(ip_address_t ip1){

if(ip1.octet_1 < 0 && ip1.octet_1 > 255){
return 0;
}

if(ip1.octet_2 < 0 && ip1.octet_2 > 255){
return 0;
}

if(ip1.octet_3 < 0 && ip1.octet_3 > 255){
return 0;
}

if(ip1.octet_4 < 0 && ip1.octet_4 > 255){
return 0;
}

return 1;
}

//2.Function to check if two IP addresses are equal:

int isequal(ip_address_t ip1, ip_address_t ip2){

if(ip1.octet_1 != ip2.octet_1){
return 0;
}

if(ip1.octet_2 != ip2.octet_2){
return 0;
}

if(ip1.octet_3 != ip2.octet_3){
return 0;
}

if(ip1.octet_4 != ip2.octet_4){
return 0;
}

return 1;

}

//3.Function to check if subnets of two IP addresses are same

int issame_subnet(ip_address_t ip1, ip_address_t ip2){

if(ip1.octet_1 != ip2.octet_1){
return 0;
}

if(ip1.octet_2 != ip2.octet_2){
return 0;
}

if(ip1.octet_3 != ip2.octet_3){
return 0;
}

return 1;

}

//4.Function to get valiid IP addresses, based on the length passed in parameters:

int get_valid_ip_addresss(ip_address_t addr_arry[], int addr_arry_len){

int cnt = 0;
int i;
for(i=0; i<addr_arry_len; i++){
char input[50];
printf("Enter:");
scanf("%s", input);

if(!strcmp(input, "-1.-1.-1.-1")){

return cnt;
}

char *splitvalue = strtok(input, ".");

int size = 0;

int a[4];

while(splitvalue != NULL){
a[size] = atoi(splitvalue);
splitvalue = strtok(NULL,".");
size++;

}

if(size == 4){

ip_address_t ip;
ip.octet_1 = a[0];
ip.octet_2 = a[1];
ip.octet_3 = a[2];
ip.octet_4 = a[3];

if(is_valid(ip)){

addr_arry[cnt] = ip;

cnt++;

}

else
printf("Invalid Input \n");

}

else{
printf("Invalid Input \n");

}

}

return cnt;

}

//5.Function to print same IP addresses one by one

void print_same_subnet(const ip_address_t addr_arry[], int addr_arry_len){

int arr1[addr_arry_len];
int i;
for(i=0; i<addr_arry_len; i++){

arr1[i] = 1;

}

int j = 0;

for(i=0; i<addr_arry_len && j<addr_arry_len; i++){
ip_address_t ip1 = addr_arry[i];

if(arr1[i] == 1){
print_ip_address(ip1);

arr1[i] = 0;

}

for(j = i+1; j <addr_arry_len; j++ ){
if(arr1[j] == 1){

ip_address_t ip2 = addr_arry[j];
if(issame_subnet(ip1,ip2)){

printf(",");

print_ip_address(ip2);

arr1[j] = 0;

}

}

}

printf("\n");

}

}

//Above are the definitions of 5 functions used in the code

Add a comment
Know the answer?
Add Answer to:
C Programming Question Hi, I have the following code and in my submission I'm supposed to...
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
  • 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"); //...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

  • I have a question on an assignment for C++. I have my code available here and...

    I have a question on an assignment for C++. I have my code available here and just want someone to look over it using MS VS 2010 express, for that is what my instructor is using. Write a program that mimics a calculator. The program should take as input two integers and the opreation to be performed. It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message.) //**This...

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

  • ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the...

    ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the attachments below and would greatly appreciate some help. ---------------------------------------            case 6:                printf("You chose determinate!\n\n");                printf("Press 2 for 2x2\n OR \n Press 3 for 3x3 Matrix: ");                scanf("%d", &detChoice);                printf("\n");                if (detChoice == 2) {                    printf("Matrix: ");...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • I am trying to figure out why my C code is outputting "exited, segmentation fault". The...

    I am trying to figure out why my C code is outputting "exited, segmentation fault". The game is supposed to generate 4 random numbers and store them in the "secret" array. Then the user is suppose to guess the secret code. The program also calculates the score of the user's guess. For now, I printed out the random secret code that has been generated, but when the game continues, it will output "exited, segmentation fault". Also, the GetSecretCode function has...

  • C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string usi...

    C Programming - RSA encryption Hi there, I'm struggling with writing a C program to encrypt and decrypt a string using the RSA algorithm (C90 language only). It can contain ONLY the following libraries: stdio, stdlib, math and string. I want to use the following function prototypes to try and implement things: /*Check whether a given number is prime or not*/ int checkPrime(int n) /*to find gcd*/ int gcd(int a, int h) void Encrypt(); void Decrypt(); int getPublicKeys(); int getPrivateKeys();...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Hello, I am having trouble with a problem in my C language class. I am trying...

    Hello, I am having trouble with a problem in my C language class. I am trying to make a program with the following requirements: 1. Use #define to define MAX_SIZE1 as 20, and MAX_SIZE2 as 10 2. Use typedef to define the following struct type: struct {     char name[MAX_SIZE1];     int scores[MAX_SIZE2]; } 3. The program accepts from the command line an integer n (<= 30) as the number of students in the class. You may assume that the...

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
Active Questions
ADVERTISEMENT