Skip to main content
gamedev.net
🔒 Locked

Another basic array C problem

Started by Blackdoor_dev May 16, 2015 at 8:23 AM 16 replies 3.7k views
Original Post
Blackdoor_dev
Blackdoor_dev

This is the program

Write a program to read the table elements into a two dimensional array temperature and to find the city and day corresponding to
(a)The highest temperature and
(b)The lowest temperature.

This is the source code-

#define MAXDAY 5

#define MAXCITY 3
#include
#include
int main(void)
{
int i,j,city,day,swap;
double temp[31][10];
char select;
printf("Enter the city temperature and the day temperature");
for(i=0;i
{
day=i+1;
for(j=0;j
city=j+1;
temp[day][city];
printf("Day= %d City=%d ",day,city);
scanf("%d",&temp[day][city]);
}
}
for(i=1;i<=MAXDAY;i++){
for(j=1;j<=MAXCITY;j++)
{
if(temp[j]>=temp[i+1][j+1]) {
swap=0;
swap=temp[j];
temp[j]=temp[i+1][j+1];
temp[i+1][j+1]=swap;
}
}
}
printf("Enter 'H' to see highest temperature or 'L' to see lowest temperature\n And X to break \n");
select=getchar();
switch('select'){
case 'H':printf("\nHighest temperature is %d in %d city in %d day",temp[1][1],j,i);
break;
case 'L':printf("\nLowest temperature is %d in %d city in %d day ",temp[5][3],j,i);
break;
if(select=='X')
{
break;}
}
getch();
return 0;
}
Error-No compilation error
Issue-Not getting what i need to.It is not accepting H and L.Even if i don't use the switch case and try to find the highest number only.Still it is not giving that.As if it is not getting sorted.
I wanted to sort that array first and then find the lowest and the highest array.
For me using loops.Character string and function.
But when it comes to using array with numerical calculations and all...I am unable to create the algorithm
wintertime
wintertime

Is that your homework? You learn more if you debug it yourself. Open the debugger, step through the program and check the values of the variables, which get changed.

Btw, the forum got code tags to make sourcecode more readable, you can click the "<>" button to get these.

haegarr
haegarr

The forum rules do not allow us to solve other peoples homework, but it allows to answer concrete questions where we can see from that you've tried hard to get a solution yourself.

Well, the program code shown in the OP has at least 10 mistakes, some of which are just flaws, some are errors, and two of them are logical mistakes regarding how a solution may work, inclusive how the correct solution may look like at all. Not wanting to discourage you, but you should restart at the problem analysis: (1) What is the goal, (2) how can it be reached with a 2D array, and (3) how can that be programmed.

As wintertime has excellently stated above, the debugger is a valuable tool. It allows you to peek into the working of computer programs, so lets you examine how statement how, how the resulting program flow is, and how variables change. Use it to your advantage!

I wanted to sort that array first and then find the lowest and the highest array.

This is possible and fine by itself, of course. But ask yourself what you get by doing so. You get the lowest and the highest temperature. The goal accordingly to (1) is to output the day and the city of two specific temperatures. With your thinking considering (2), the indices of the array are used to denote the specific days and cities, resp. You associate the belonging temperature with a specific day and city by writing the temperature value into the array place that is addressed by that indices; that is fine so far. Now, what happens to that association if you sort the array as is shown in the OP?

Blackdoor_dev
Blackdoor_dev

The forum rules do not allow us to solve other peoples homework, but it allows to answer concrete questions where we can see from that you've tried hard to get a solution yourself.

Well, the program code shown in the OP has at least 10 mistakes, some of which are just flaws, some are errors, and two of them are logical mistakes regarding how a solution may work, inclusive how the correct solution may look like at all. Not wanting to discourage you, but you should restart at the problem analysis: (1) What is the goal, (2) how can it be reached with a 2D array, and (3) how can that be programmed.

As wintertime has excellently stated above, the debugger is a valuable tool. It allows you to peek into the working of computer programs, so lets you examine how statement how, how the resulting program flow is, and how variables change. Use it to your advantage!

I wanted to sort that array first and then find the lowest and the highest array.

This is possible and fine by itself, of course. But ask yourself what you get by doing so. You get the lowest and the highest temperature. The goal accordingly to (1) is to output the day and the city of two specific temperatures. With your thinking considering (2), the indices of the array are used to denote the specific days and cities, resp. You associate the belonging temperature with a specific day and city by writing the temperature value into the array place that is addressed by that indices; that is fine so far. Now, what happens to that association if you sort the array as is shown in the OP?

And this is not my homework.
I am self studying this

Debugger in dev cpp is not working on my Windows 8.1 64 bit.

haegarr
haegarr




I am self studying this

Okay, then you not should but want to solve the problem by yourself. :) So what is your concrete question regarding the issues I've already noted?



Debugger in dev cpp is not working on my Windows 8.1 64 bit.

Then you shall think to turn to another developer environment. You definitely will need a debugger. The given situation is only the first of many others that will come ...

Blackdoor_dev
Blackdoor_dev

I am self studying this

Okay, then you not should but want to solve the problem by yourself. smile.png So what is your concrete question regarding the issues I've already noted?


Debugger in dev cpp is not working on my Windows 8.1 64 bit.

Then you shall think to turn to another developer environment. You definitely will need a debugger. The given situation is only the first of many others that will come ...

Any suggestion for windows 8.1 64 bit?

Good and a bad thing about programming is..I can't use my brain in anything else until i figure out what is wrong with me program

Agony
Agony

Visual Studio Community 2013 is full featured and has a very high quality debugger.

"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
wintertime
wintertime
... and it will deactivate a month after installing it and tell you to get a MS account.
Though you could also try out code::blocks or one of the many other compilers, IDEs and text editors, which are available.
rip-off
rip-off
It is important not to be intimidated by the more complex interface of a modern and full-featured IDE like Visual Studio. It was a mistake I made when I was starting out. Having good tools is vital.

As for your program, there are definitely a few things the compiler should be noticing for you. Look into configuring your build tools to have a very high warning level (ideally, all warnings) and also to treat warnings as errors. Particularly as a beginner, any warning is very likely to be an issue you should fix.

When I compile your program (slightly modified for Linux), I get the following output:

$ gcc gamedev.c -Wall -Werror
gamedev.c: In function ‘main’:
gamedev.c:16:1: error: statement with no effect [-Werror=unused-value]
 temp[day][city];
 ^
gamedev.c:19:1: error: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘double *’ [-Werror=format=]
 scanf("%d",&temp[day][city]);
 ^
gamedev.c:37:8: error: character constant too long for its type [-Werror]
 switch('select'){
        ^
gamedev.c:38:1: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Werror=format=]
 case 'H':printf("\nHighest temperature is  %d in %d city in %d day",temp[1][1],j,i);
 ^
gamedev.c:40:1: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Werror=format=]
 case 'L':printf("\nLowest temperature is %d in %d city in %d day ",temp[5][3],j,i);
 ^
cc1: all warnings being treated as errors
These should be simple enough to fix, but see if you can configure your environment to give you a similar set of warnings - the exact warnings offered vary between compilers, do don't worry if you don't get them all or see other ones.

Be consistent with your formatting and indentation. It is hard to know because without the proper tags, much of the indentation was lost, but a simple example is where you place the curly braces after a loop or conditional statement. The two most common conventions are on the same line (possibly with whitespace preceding it), or on the next line. It doesn't matter which one you pick, but it does matter that you pick one and be consistent.

Also, another thing that will make your code easier to read is to use more whitespace around expressions. Again, the exact details can vary, but almost everyone would agree that this:

for(i=1;i<=MAXDAY;i++)
{
    for(j=1;j<=MAXCITY;j++)
    {
        if(temp[i][j]>=temp[i+1][j+1])
        {
            swap=0;
            swap=temp[i][j];
            temp[i][j]=temp[i+1][j+1];
            temp[i+1][j+1]=swap;
        }
    }
}
Is a bit harder to read than this:

for (i = 1; i <= MAXDAY; i++)
{
    for (j = 1; j <= MAXCITY; j++)
    {
        if (temp[i][j] >= temp[i + 1][j + 1])
        {
            swap = 0;
            swap = temp[i][j];
            temp[i][j] = temp[i + 1][j + 1];
            temp[i + 1][j + 1] = swap;
        }
    }
}
Typically I'd put a space between function parameters too, for example:

// Instead of:
printf("Day= %d City=%d ",day,city);
// Consider
printf("Day= %d City=%d ", day, city);
Even with all this, you won't have fixed everything, but you'll be much better placed to solve the remaining issue.
Blackdoor_dev
Blackdoor_dev

@rip-off

Thanks for helping me out bro.Ill right code clearer from now. :)
You are great!

I solved all the loops from the exercise including the hard ones.And even solved some character array(String).
But in numerical array i stuck badly...and loosing my motivation :c

@

haegarr

Thanks man for helping me :D

Ill try to change the environment from Decpp.

@all the other people who helped me

Thanxxxxxxxxxxxxxxxxxxxxxxxxx :)

nobodynews
nobodynews




... and it will deactivate a month after installing it and tell you to get a MS account.

Getting an "MS account" is free. If you still don't want to get an MS account that's your prerogative, just don't want to scare people off who don't care about getting an account.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
wintertime
wintertime
You read more into it than was intended (me writing a few more words would have been better). Its just an unnecessary annoyance built in there, which I thought was worth mentioning upfront, so he would not be surprised later, in case he cares.
Brain
Brain
All the free editions of visual studio, e.g. express, community, have always required that the user have a passport "live" account to register the program.

In all honesty who doesn't have an MSN account these days, as they are used for Skype, xbox live, MSDN, office 365, windows phone, and just about everything ms offer...
BitMaster
BitMaster

In all honesty who doesn't have an MSN account these days, as they are used for Skype, xbox live, MSDN, office 365, windows phone, and just about everything ms offer...


I'm not using any of that outside work and I would not use work accounts for private endeavors.
WozNZ
WozNZ

... and it will deactivate a month after installing it and tell you to get a MS account.
Though you could also try out code::blocks or one of the many other compilers, IDEs and text editors, which are available.

Why is it a problem to create a free account to get one of the best IDEs out there. The community edition is the full blown version not the old crippled express editions.

I have been using for months with the full resharper stack installed into it (Resharper is also free if you are involved in an open source project)

Can't wait for the new 2015 edition when released :)

jwezorek
jwezorek

blah blah blah .. dev cpp ... blah blah blah ...Windows 8.1 64 bit.


Well there's your problem.

If you are not writing something that needs to be cross-platform there is no reason whatsoever to not use Visual Studio Community edition. I don't know what wintertime is concerned about above: a "Microsoft account" is just a banal free account that you might even already have if say, you, ever set up an outlook.com or hotmail email address. Anyway it takes about a minute to create an account and is free.

If you do care about being cross-platform there is still no reason to use Dev-C++ which hasn't been updated in a decade at this point.

BitMaster
BitMaster

If you are not writing something that needs to be cross-platform there is no reason whatsoever to not use Visual Studio Community edition. I don't know what wintertime is concerned about above: a "Microsoft account" is just a banal free account that you might even already have if say, you, ever set up an outlook.com or hotmail email address. Anyway it takes about a minute to create an account and is free.

I would not agree with that. I'm using MSVC at work and I'm now using QtCreator with MinGW at home for my hobby. The only thing I miss at home are the debugging tools MSVC has (although QtCreator certainly has become more comfortable since I have started using it). For everything else I need QtCreator (and the compiler with extremely decent C++11/C++14 support) is doing an equivalent or better job.
That said, for someone new to C++ I would still recommend to start out with some kind of reasonable MSVC. It's simpler to find help on the weband especially precompiled libraries.

If you do care about being cross-platform there is still no reason to use Dev-C++ which hasn't been updated in a decade at this point.

While I can agree on the verdict (avoid Dev-C++ at all cost), the statement as given is not completely correct. Bloodshed Dev-C++ has not been updated in more than a decade, but there are two or three much newer forks around (although I think one of them is a bit abandoned again). However, I still would not advise anyone to use any Dev-C++ at this point. Code::Blocks and QtCreator are decent alternatives to MSVC (still not what I would suggest to a newbie, though, as said above).
jwezorek
jwezorek

I like QtCreator and it is what I would recommend for general cross-platform if you want to use an IDE or if you are developing to Qt specifically, of course. Beyond that I still see no reason to not use Visual Studio Community Edition unless maybe you are already familiar with QtCreator for some reason but are not familiar with VS.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.