Return of the C!
To truly master and understand a language’s core essence, you need to interact with the language on a daily basis just like you speak a human language. Since C became one of my coding universe’s integral components, I want to keep in touch with the legendary language! Here’s the brute force solution I implemented in C.
To truly master a programming language, you may need to keep interacting with the language. If you’ve kept track of my recent blogging activities, you may know that I’ve been updating my blog almost four or five times a day. Why? Well, somehow I started realizing my own capabilities and there’s no turning point but to keep dedicating myself to the tech world.
So many of my personal events gave me a dangerous level of motivation to improve myself and my tech skills. I simply have no idea how much longer I can keep this active learning motivation, but what I can tell for sure is that my mental health status is way much better than ever before.
The less I suffer from my past traumatic experience, the more I spend time thinking about how I can implement the idea into actual coding and apps.
So, here, throughout this new c-language challenge series, I want to keep track of my interactions with the legacy language. As you may know, I’ve been posting another coding challenge series, Road to Google, and aside from that, I want to implement what I did there to C language as well.
Also, since I finished a C-language series as well as my self-learning process of it, Learn C Language, a couple of days back, it’s the best time to improve my coding skill in the legendary language.
So, here I present you, a whole new C-language series – Be Excel in C Language!
Question:
Given an array of integers, return the indices of the two numbers that add up to a given target.
What is this?
So, this is what I wrote in one of my previous posts: Road to Google – Part 1: Array and Brute Force. Since I already wrote down the explanations of what this question asks you, you can read it there if you want.
And here, I rewrote what I did in Java in C.
Since C is quite similar to Java in terms of syntax (more specifically, Java is built based on C), both coding solutions are almost identical. Noticeable differences I was most interested in were the following parts:
Difference from Java 1
When an array is passed to a function, it decays into a pointer (line 3) and the array’s size is lost at that very moment. So, you have to specify the size of the array as I’ve shown in image 01’s line 4.
In line 3, the array is passed as a pointer.
void findPair(int *nums, int targetNum)
And you have to specify the size of the array,
nums[5];
Difference from Java 2
In Java, when you want to access the size of the array in a for-loop, one possible way in Java is to write it as nums.length.
for(int j = i + 1; j < nums.length; j++)
On the other hand, in C, you must write it as sizeof(nums).
for (int i = 0; i < sizeof(nums) - 1; i++)
Those are two noticeable differences I noticed when coming in C. While I was coding, I faced a number of errors and googled multiple times until I found the solution. As always, really appreciate StackOverflow bros!
Actual code:
#include <stdio.h>
void findPair(int *nums, int targetNum) {
nums[5];
for (int i = 0; i < sizeof(nums) - 1; i++) {
for (int j = i + 1; j < sizeof(nums); j++) {
if (nums[i] + nums[j] == targetNum) {
printf("Pair found (%d, %d)", nums[i], nums[j]);
return;
}
}
}
printf("Pair not found");
}
void main() {
int nums[5] = {1,3,7,9,2};
int targetNum = 11;
findPair(nums, targetNum);
}
Anyways, see you soon!