计算2D数组中的值 - C.

时间:2017-05-24 11:00:34

标签: c xcode windows macos sublimetext3

所以我一直在尝试通过学习C的作业,并且只是希望对我出错的地方进行某种修正

该情景是一项调查,这是我预计会产生的输出

import { Http, Request, RequestOptionsArgs } from "@angular/http";
import { RouterModule, Router } from "@angular/router";


class HttpMock extends Http {

    get(url: string, options?: RequestOptionsArgs): any {

    }

    request(url: string | Request, options?: RequestOptionsArgs): any {

    }
}

fdescribe('Tag Effect', () => {
    let runner: EffectsRunner;
    let navEffects: NavigationServiceEffect;
    let navService: NavigationService;
    let actions: Actions;

    beforeEach(() => TestBed.configureTestingModule({
        imports: [
            EffectsTestingModule,
            RouterModule,
            StoreModule.provideStore(reducers())
        ],
        providers: [
            Actions,
            NavigationService,
            HttpService,
            { provide: Http, useClass: HttpMock },
            { provide: Router, useClass: class { navigate = jasmine.createSpy("navigate"); } }
        ]
    }));

    it('Call Load Navigation items action after Trigger Load aciton',
        inject([
            Actions, NavigationService
        ],
            ( _actions, _navService) => {
                actions = _actions;
                navService = _navService;

                spyOn(navService, 'load')
                    .and.returnValue(Observable.of([
                        { "name": "Help", "hasChild": false, "roles": [101, 151, 201, 301, 401], "url": "help" }
                    ]));


                navEffects = new NavigationServiceEffect(actions, navService);

                navEffects.navigations$.subscribe(r => console.log(r));

                navEffects.navigations$.subscribe(result => {

                    console.log(result);
                    expect(result.type).toEqual(NavigationActionType.LOAD_NAVIGATIONS_LINKS);
                    expect(result.payload.length).toEqual(3); // This should fail

                });
            }));

});

表格的变量是M,S,B,A和1,2,3,4

每次我执行我的代码时,它都不会对这些值进行计数,或者说是一个值为36的

     POPULARITY TABLE
     ----------------------------------------------------
     City Cricket Footy Tennis Soccer
     -----------------------------------------------------
     Melbourne 4 1 1 4
     Sydney 4 5 1 0
     Brisbane 2 2 4 4
     Adelaide 1 0 2 0
    ----------------------------------------------------- 

我哪里出错了?有没有更好的方法来编写代码呢?

我只为墨尔本编码,因为在编码时,我的逻辑是,如果我可以让一个城市工作,其余的将是直截了当的

2 个答案:

答案 0 :(得分:1)

有两件事突出:

  • 您使用未初始化的字符变量(MBSA)就像它们意味着什么一样;这非常令人困惑。
  • 这一行:if (survey[i][j] == M, 1)毫无意义。它始终采用if,因为逗号运算符使条件等效于单个1

答案 1 :(得分:1)

这个循环...

for (int j = 0; j == 2; j++)

......什么都不做。它仅在j2时运行。由于它初始化为0,情况从未如此,所以内部的代码永远不会被执行。

如果您希望它循环遍历内部数组(01)的值,您想要检查j是否小于2,就像你已完成外循环一样。

for (int j = 0; j < 2; j++)

虽然看起来你似乎试图在这些循环中的if中同时检查这两个值,但是你想要做的是用这样的不同if语句替换该循环?

if ((survey[i][0] == M) && (survey[i][1]== 1))

此外,如果您打算使用MBSA变量将字符放入数组中,您只需要输入单引号与'M'类似,目前它们没有分配值,因此可能具有任何值。同样也适用于if语句。

总而言之,您的代码应如下所示:

#include  <stdio.h>
int main()
   {
   int m_cricket = 0, m_footy = 0, m_tennis = 0, m_soccer = 0;
   int s_cricket = 0, s_footy = 0, s_tennis = 0, s_soccer = 0
   int b_cricket = 0, b_footy = 0, b_tennis = 0, b_soccer = 0
   int a_cricket = 0, a_footy = 0, a_tennis = 0, a_soccer = 0
   int survey[36][2] = {{'M', 1}, {'S', 2}, {'B', 1}, {'B', 3}, {'M', 2}, {'B', 4}, {'S', 1}, {'A', 3}, {'M', 4}, {'B', 2}, {'B', 1}, {'S', 3}, {'B', 4}, {'B', 4}, {'M', 1}, {'M', 1}, {'B', 3}, {'B', 3}, {'S', 1}, {'S', 1}, {'S', 2}, {'M', 4}, {'M', 4}, {'S', 2}, {'A', 1}, {'S', 2}, {'B', 3}, {'M', 1}, {'B', 1}, {'S', 2}, {''A'', 3}, {'M', 4}, {'S', 1}, {'B', 2}, {'M', 3}, {'B', 4}};

   for (int i = 0; i < 36; i++)
      {
      if ((survey[i][0] == 'M') && (survey[i][1]==1))
         {
         m_cricket++;
         }
      }

    printf("\tCity  Cricket Footy  Tennis  Soccer\n");
    printf("------------------------------------------------\n");
    printf("Melbourne:\t %d\t %d\t %d\t %d\t\n", m_cricket, m_footy, m_tennis, m_soccer);
    printf("Sydney:\t %d\t %d\t %d\t %d\t\n", s_cricket, s_footy, s_tennis, s_soccer);
    printf("Brisbane:\t %d\t %d\t %d\t %d\t\n", b_cricket, b_footy, b_tennis, b_soccer);
    printf("Adelaide:\t %d\t %d\t %d\t %d\t\n", a_cricket, a_footy, a_tennis, a_soccer);

    return 0;
}