What's new

Need Help with C (Programming Language)

ahmedwaqas92

ODI Debutant
Joined
Dec 26, 2013
Runs
10,582
Post of the Week
4
Hey Guys!! ok so the task is very simple and extremely minuscule (for any half decent C programmer)

I need to write a program that will

1) Define an integer called 'factor' and place its value = 1.0
2) Store a value to another integer called 'x' (This is user input value)
3) Check to see if the input value is less than 500 if it is then 'x' multiplies with 'factor' and produces a result with another integer that will be used later.
4) If value of 'x' is > 500 then program should do (x - 500) = store in another integer let's say 'a', concurrently the program should also store (multiply 500 x 'factor' as well)
5) For 'a' the program should check if a < 700 if it is then it should reduce the 'factor' by 20% and multiply with 'a'.
6) If a > 700 then the program should do (a-700) = store the integer in 'b', concurrently the program should also store (multiply 700 x 20% reduced of 'factor')
7)The remaining value should then be multiplied with a 20% reduced factor of the factor reduction I did in step 5.
8) Loop this sequence (step 5-Step7) until x is less than 700 and we have a total based on that. (Factor needs to be reduced by 20% on the last stored value after every set of 700 for 'x' - until the loop finishes).
9) Once we have this figure based on whatever condition it suffices I need to add it to 'x' times 0.05 and output a result on the screen


P.S. This is a pricing model basically, just in case you were wondering.

Help Needed
Thanks Guys
Waqas
 
One of the Sachin posters may be able to help you, he's good at programming can't remember his tag
 
Not a programmer... but I was taught integers don't have decimals :P
 
One of the Sachin posters may be able to help you, he's good at programming can't remember his tag

I think it's [MENTION=139108]Sachin136[/MENTION]

I can only help you a bit with Matlab ahmedwaqas :))
 
Not a programmer... but I was taught integers don't have decimals :P

Really, am sure you would have done a computing module and used Matlab / Python for some of your projects
 
[MENTION=3474]TalhaSyed[/MENTION] Are you familiar with the VHDL programming language ?
 
I lost it at condition 4 the whole procedure seems too confusing to code. Share couple of examples if you can't put up in text.



Tried to code what you said so far, here it is:

#include <stdio.h>

int main()
{
float f = 1.0f;
float x = 0;
float a = 0;
float b = 0;

printf("Enter : x\n");
scanf("%d" , &x);

if(x<500)
{
f=x*f;
}else {
a=x-500;
}

while(x<700)
{
if(a<700)
{
f=a*(f/5);
} else {
b=a-700;
f=a*(f/5);
}
x-=700;
}

x*=0.5;
printf("X: %f", x);

return 0;
}
 
Really, am sure you would have done a computing module and used Matlab / Python for some of your projects

Yes and used C quite a bit... but cannot compare to people who do programming day in and day out as part of their day job
 
For 5-8, what if x is less than 500 from the start? What's the remaining value in 7? Maybe use variable names for every thing you need to store so it is easier to follow.
 
For 5-8, what if x is less than 500 from the start? What's the remaining value in 7? Maybe use variable names for every thing you need to store so it is easier to follow.

what if x and a are equal to their respective values? :D
 
Hey Guys!! ok so the task is very simple and extremely minuscule (for any half decent C programmer)

I need to write a program that will

1) Define an integer called 'factor' and place its value = 1.0
2) Store a value to another integer called 'x' (This is user input value)
3) Check to see if the input value is less than 500 if it is then 'x' multiplies with 'factor' and produces a result with another integer that will be used later.
4) If value of 'x' is > 500 then program should do (x - 500) = store in another integer let's say 'a', concurrently the program should also store (multiply 500 x 'factor' as well)
5) For 'a' the program should check if a < 700 if it is then it should reduce the 'factor' by 20% and multiply with 'a'.
6) If a > 700 then the program should do (a-700) = store the integer in 'b', concurrently the program should also store (multiply 700 x 20% reduced of 'factor')
7)The remaining value should then be multiplied with a 20% reduced factor of the factor reduction I did in step 5.
8) Loop this sequence (step 5-Step7) until x is less than 700 and we have a total based on that. (Factor needs to be reduced by 20% on the last stored value after every set of 700 for 'x' - until the loop finishes).
9) Once we have this figure based on whatever condition it suffices I need to add it to 'x' times 0.05 and output a result on the screen


P.S. This is a pricing model basically, just in case you were wondering.

Help Needed
Thanks Guys
Waqas

Didnt know people are still using C :D
 
I'm only familiar with PHP and JavaScript. A bit of Java also. Will read the documentation of C once I'm home to see if I can figure this out.
 
I may need to learn it soon so I can enhance me resume, what learning resources would you recommenced for a newbie to the language

My professor wrote a book on VHDL it was very good, highly recommended. Look for VHDL for Engineers by Kenneth L Short on Amazon. The only challenge I see newbie having is getting the hardware, syntax wise, it is very similar to C, I used Aldec HDL IDE.
 
There is more chance of someone helping here than at SO, if you haven't shown what you have tried so far, and just want someone else to do your homework.

Coding in C is not really an issue for me, but he need to clarify the problem atleast. There's no connection in steps like what is being upgraded and where.


Didnt know people are still using C :D

Its mainly used for linux/variants and embedded programming these days.
 
[MENTION=136588]CricketCartoons[/MENTION] [MENTION=145660]maxamax[/MENTION] [MENTION=138254]Syed1[/MENTION] [MENTION=139288]Abdul[/MENTION] [MENTION=132973]sjahmed23[/MENTION] [MENTION=93712]MenInG[/MENTION] [MENTION=46929]shaz619[/MENTION] [MENTION=139108]Sachin136[/MENTION]

Sorry about the delayed response guys...Work has been murderous

Ok, let me get a few things out of the way so that there is some perspective to this query.

1) This is not a homework, I just need someone to help me put together a few lines of code (if possible) that I can run on my Free BSD back end server/terminal - That's basically a Unix distro that we are running as back end for all our ERP/Big Data/Numeric Analaysis.

If you're not comfortable sharing the code (I understand) just give me a working flow or point me in the right direction using a simple Algorithm, Anything that I might translate into command line syntax and produce a result on the terminal. This is also the reason why I am using native C (Legacy code) as I can then source this to any of my staff, whoever might be working on the project that I will explain below.

2) This is actually a pricing model that a customer of ours tried explaining to us. This is how the pricing model goes - In plain English....

(A) The customer provides a service let's assume the service is 'ABC' - I can't name the service due to copyright and other legal issues.
(B) For this service the customer first asks's their clientele on how much is the 'input_value'. The 'input_value' can be anything >0 (simple enough) - Can only be integer while input (result has to be decimal)
(C) The program then checks if this 'input_value' is <500
(I) If the 'input_value' is < 500 then:
(i) 'input_value' multiplied with 'factor' that has a starting position of 1.0
(ii) When condition (i) is completed, the system should ask the user does 'feature sn' apply on this
input, if user press 'y' the program should multiply 'input_value' with 0.05 and then add this
result with (i) and produce a float (decimal value) on the screen. If User presses 'n' skip (ii)
and just produce whatever result is stored in (i).
(II) If 'input_value' is NOT < 500:
(i) Subtract 500 from 'input value' and multiply the 500 with factor 1.0
(ii) Check to see if remaining 'input_value' is < 700 if it is then multiply the ('input value' - 500)
with a reduced 'factor' by 20% which means 20% of 1.0 (factor) is 0.8
(iii) If 'input_value' (after subtracting intial 500) is still > 700 then do (['input value' - 500] - 700),
multiply 700 with reduced 20% of 'factor' i.e. 0.8 (as explained above), proceed to reduce the
'factor' by another 20% (i.e. 20% reduction of 0.8 after which we get 0.64), multiply this
'factor' with (['input_value' - 500] - 700) and then do a check again for point number (ii)
(iv) Loop this until 'input_value' finishes and has a set below 700
(v) The above result should then go through a 'feature sn' check, if user press 'y' then original
'input_value' multiples with 0.05 + all the above calculation and result displayed on the screen
however if user press 'n' the programs skips this and just produces a result based on above
calculation from (i) - (iv)

Working Scenario (Example)

Let's say user input is 1800

Step 1 - 500 (first 500 set from 'user_input') x 1.0 (factor) = 500
Step 2 - 700 (Second 700 set from Step 1) x 0.8 (reduced factor by 20% of above) = 560
Step 3 - 600 (The remaining value < 700 from Step 2) x 0.64 (reduced factor by 20% of above) = 384

Since the complete block of 'user_input' has finished we now do a 'feature sn' check so if 'feature sn' is yes then 1800 x 0.05 = 90 and add to the total result i.e. output should be 500+560+384+90 = 1534. If 'feature sn' is no then just display result as 500+560+384 = 1444

-----------------------------------------------------------------------------------------------------------------

Some of my staff are using windows systems ranging from xp/7/8.1/10 to Linux Unbuntu to BSD terminals such as me, therefore I need to have legacy C code as I can use any terminal/shell/ or command prompt to get a result for this type of input. Server is hosted on a BSD as well.

My job is not related to this coding at all, the problem is that we got existing financial numericals and marketing analysis from the customer that used this pricing model on a manual entry basis using nothing but manual workforce that would crunch in numbers on a calculator, reverify it by hand, load the entry into their ERP software and then evaluate their targets and bugets based on this :facepalm:

The job is complicated because I am not too sure if all the input is error free which is why my analysis right at the end, which is a mesh of predictive analysis using progressions and Big data velocity, can go horribly wrong if my input is not matching with the numbers I plugging into SPSS, Octave or Matlab for that matter.

That's the whole gimmick here. I know at the end I too have to manually see whether each input is correct of not by comparing it one by one but to make it a bit easier for everyone involved I need a quick fix on how to verify if the value being plugged into the ERP module is the correct one or not. Based on the calculation that is done above.

Any help on the matter would be much appreciated.
Thanks Guys
Waqas
 
[MENTION=136588]CricketCartoons[/MENTION] [MENTION=145660]maxamax[/MENTION] [MENTION=138254]Syed1[/MENTION] [MENTION=139288]Abdul[/MENTION] [MENTION=132973]sjahmed23[/MENTION] [MENTION=93712]MenInG[/MENTION] [MENTION=46929]shaz619[/MENTION] [MENTION=139108]Sachin136[/MENTION]

Sorry about the delayed response guys...Work has been murderous

Ok, let me get a few things out of the way so that there is some perspective to this query.

1) This is not a homework, I just need someone to help me put together a few lines of code (if possible) that I can run on my Free BSD back end server/terminal - That's basically a Unix distro that we are running as back end for all our ERP/Big Data/Numeric Analaysis.

If you're not comfortable sharing the code (I understand) just give me a working flow or point me in the right direction using a simple Algorithm, Anything that I might translate into command line syntax and produce a result on the terminal. This is also the reason why I am using native C (Legacy code) as I can then source this to any of my staff, whoever might be working on the project that I will explain below.

2) This is actually a pricing model that a customer of ours tried explaining to us. This is how the pricing model goes - In plain English....

(A) The customer provides a service let's assume the service is 'ABC' - I can't name the service due to copyright and other legal issues.
(B) For this service the customer first asks's their clientele on how much is the 'input_value'. The 'input_value' can be anything >0 (simple enough) - Can only be integer while input (result has to be decimal)
(C) The program then checks if this 'input_value' is <500
(I) If the 'input_value' is < 500 then:
(i) 'input_value' multiplied with 'factor' that has a starting position of 1.0
(ii) When condition (i) is completed, the system should ask the user does 'feature sn' apply on this
input, if user press 'y' the program should multiply 'input_value' with 0.05 and then add this
result with (i) and produce a float (decimal value) on the screen. If User presses 'n' skip (ii)
and just produce whatever result is stored in (i).
(II) If 'input_value' is NOT < 500:
(i) Subtract 500 from 'input value' and multiply the 500 with factor 1.0
(ii) Check to see if remaining 'input_value' is < 700 if it is then multiply the ('input value' - 500)
with a reduced 'factor' by 20% which means 20% of 1.0 (factor) is 0.8
(iii) If 'input_value' (after subtracting intial 500) is still > 700 then do (['input value' - 500] - 700),
multiply 700 with reduced 20% of 'factor' i.e. 0.8 (as explained above), proceed to reduce the
'factor' by another 20% (i.e. 20% reduction of 0.8 after which we get 0.64), multiply this
'factor' with (['input_value' - 500] - 700) and then do a check again for point number (ii)
(iv) Loop this until 'input_value' finishes and has a set below 700
(v) The above result should then go through a 'feature sn' check, if user press 'y' then original
'input_value' multiples with 0.05 + all the above calculation and result displayed on the screen
however if user press 'n' the programs skips this and just produces a result based on above
calculation from (i) - (iv)

Working Scenario (Example)

Let's say user input is 1800

Step 1 - 500 (first 500 set from 'user_input') x 1.0 (factor) = 500
Step 2 - 700 (Second 700 set from Step 1) x 0.8 (reduced factor by 20% of above) = 560
Step 3 - 600 (The remaining value < 700 from Step 2) x 0.64 (reduced factor by 20% of above) = 384

Since the complete block of 'user_input' has finished we now do a 'feature sn' check so if 'feature sn' is yes then 1800 x 0.05 = 90 and add to the total result i.e. output should be 500+560+384+90 = 1534. If 'feature sn' is no then just display result as 500+560+384 = 1444

-----------------------------------------------------------------------------------------------------------------

Some of my staff are using windows systems ranging from xp/7/8.1/10 to Linux Unbuntu to BSD terminals such as me, therefore I need to have legacy C code as I can use any terminal/shell/ or command prompt to get a result for this type of input. Server is hosted on a BSD as well.

My job is not related to this coding at all, the problem is that we got existing financial numericals and marketing analysis from the customer that used this pricing model on a manual entry basis using nothing but manual workforce that would crunch in numbers on a calculator, reverify it by hand, load the entry into their ERP software and then evaluate their targets and bugets based on this :facepalm:

The job is complicated because I am not too sure if all the input is error free which is why my analysis right at the end, which is a mesh of predictive analysis using progressions and Big data velocity, can go horribly wrong if my input is not matching with the numbers I plugging into SPSS, Octave or Matlab for that matter.

That's the whole gimmick here. I know at the end I too have to manually see whether each input is correct of not by comparing it one by one but to make it a bit easier for everyone involved I need a quick fix on how to verify if the value being plugged into the ERP module is the correct one or not. Based on the calculation that is done above.

Any help on the matter would be much appreciated.
Thanks Guys
Waqas

Not sure if it works correctly (I still don't understand how your model works) but it works for your example. https://pastebin.com/cra3Aj5i
 
My professor wrote a book on VHDL it was very good, highly recommended. Look for VHDL for Engineers by Kenneth L Short on Amazon. The only challenge I see newbie having is getting the hardware, syntax wise, it is very similar to C, I used Aldec HDL IDE.

The syntax is not hard to learn, but are you already familiar with circuit/digital design?

http://freerangefactory.org/

"Circuit Design and Simulation with VHDL" by Pedroni

Thanks guys and yes Sachin I need to get familiar with circuit design, am cool with their fundamental operation but not their design and functionality with regards to VHDL so it's something I plan on using whatever useful learning resources I can find to become somewhat decent at it, not on the expert level obviously as that takes years
 
[MENTION=3474]TalhaSyed[/MENTION] Are you familiar with the VHDL programming language ?

Nah man unfortunately not.

I did some C programming for the first couple of years of University but that was a while ago and a distant memory now :afridi
 
Nah man unfortunately not.

I did some C programming for the first couple of years of University but that was a while ago and a distant memory now :afridi

Just was asking because electrical engineers tend to use it a fair amount :ksi
 
You just want the answers. You will never learn it this way. This is the main reason people are frustrated with coding. They just want answers.
 
Just was asking because electrical engineers tend to use it a fair amount :ksi

The ones who know what they are doing tend to use it a lot man :))

Nah I think it's used a lot more by electronic engineers rather than electrical engineers - but I may be mistaken :)
 
The ones who know what they are doing tend to use it a lot man :))

Nah I think it's used a lot more by electronic engineers rather than electrical engineers - but I may be mistaken :)

What's the difference between electronic and electrical :yk , seems like a bit of a similarity/distinction between aerospace and aeronautical
 
What's the difference between electronic and electrical :yk , seems like a bit of a similarity/distinction between aerospace and aeronautical

So electronic engineers work on electronic devices (e.g. computers, smart phones, antennas, tablets, electronics within cars, analogue and digital electronics etc. to name a few), whilst electrical engineers deal with large scale power (e.g. transmission grids, power plants, buildings, renewable energy etc.)
 
So electronic engineers work on electronic devices (e.g. computers, smart phones, antennas, tablets, electronics within cars, analogue and digital electronics etc. to name a few), whilst electrical engineers deal with large scale power (e.g. transmission grids, power plants, buildings, renewable energy etc.)

Fair enough makes sense, hows everything going with you bro all good
 
Fair enough makes sense, hows everything going with you bro all good

Yea man alhumdullilah keeping well!

Got a week off work next week so looking forward to that inshallah :)

How about you?
 
Yea man alhumdullilah keeping well!

Got a week off work next week so looking forward to that inshallah :)

How about you?

That's good bro and all good, you can watch extra footage of the big dawg during that time :))
 
Back
Top